Diameter of binary tree
July 14, 2022
treeProblem URL: Diameter of binary tree
We can calculate the depth of a subtree with DFS. Then we add both subtree and store it in our running answer. After the whole tree traversal is done, we return our running answer.
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def diameterOfBinaryTree(self, root: Optional[TreeNode]) -> int:
self.answer = 0
def height(root):
if not root:
return 0
left, right = height(root.left), height(root.right)
self.answer = max(self.answer, left+right)
return 1 + max(left, right)
height(root)
return self.answer
Time Complexity: O(n)
Space Complexity: O(n)