Diameter of Binary Tree

 

  1. Clarify the problem:

    • The problem requires finding the diameter of a binary tree.
    • The diameter of a binary tree is defined as the maximum number of edges between any two nodes in the tree.
    • We need to implement a function that takes the root of a binary tree as input and returns the diameter of the tree.
  2. Analyze the problem:

    • Input: The root node of a binary tree.
    • Output: The diameter of the binary tree (an integer).
    • Constraints:
      • The binary tree can be empty.
      • The binary tree can have at most 10^4 nodes.
  3. Design an algorithm:

    • We can solve this problem using a recursive approach.
    • We need to calculate the height of the left and right subtrees for each node.
    • The diameter of the binary tree passing through a particular node is the sum of the heights of its left and right subtrees.
    • We traverse the tree recursively and update the diameter at each node.
  4. Explain your approach:

    • We will implement a function called diameterOfBinaryTree that takes the root of a binary tree as input.
    • If the root is None, we return 0 as the diameter.
    • Otherwise, we recursively calculate the height of the left and right subtrees.
    • We calculate the diameter passing through the current node by summing the heights of the left and right subtrees.
    • We then recursively calculate the diameter for the left and right subtrees and take the maximum of these values.
    • Finally, we return the maximum diameter.
  5. Write clean and readable code:

    python
  6. class TreeNode: def __init__(self, val=0, left=None, right=None): self.val = val self.left = left self.right = right def diameterOfBinaryTree(root): def height(node): if not node: return 0 left_height = height(node.left) right_height = height(node.right) return max(left_height, right_height) + 1 def diameter(node): if not node: return 0 left_diameter = diameter(node.left) right_diameter = diameter(node.right) return max(left_diameter, right_diameter, height(node.left) + height(node.right)) return diameter(root)
  7. Test your code:

    python
  8. # Test case 1 # Input: # 1 # / \ # 2 3 # / \ # 4 5 # The diameter is the path 4 -> 2 -> 1 -> 3 with a length of 3. # Expected output: 3 root = TreeNode(1) root.left = TreeNode(2) root.right = TreeNode(3) root.left.left = TreeNode(4) root.left.right = TreeNode(5) assert diameterOfBinaryTree(root) == 3 # Test case 2 # Input: # 1 # / \ # 2 3 # / \ \ # 4 5 6 # / / \ # 7 8 9 # The diameter is the path 7 -> 4 -> 2 -> 1 -> 3 -> 6 -> 9 with a length of 6. # Expected output: 6 root = TreeNode(1) root.left = TreeNode(2) root.right = TreeNode(3) root.left.left = TreeNode(4) root.left.right = TreeNode(5) root.left.left.left = TreeNode(7) root.right.right = TreeNode(6) root.right.right.left = TreeNode(8) root.right.right.right = TreeNode(9) assert diameterOfBinaryTree(root) == 6
  9. Optimize if necessary:

    • The solution already has an efficient time complexity, as we visit each node only once.
    • There is no need for further optimization.
  10. Handle error cases:

    • The code handles the case where the root is None by returning 0 as the diameter.
  11. Discuss complexity analysis:

    • The time complexity of the solution is O(n), where n is the number of nodes in the binary tree. We visit each node once.
    • The space complexity is O(h), where h is the height of the binary tree. This is the space required for the recursive call stack. In the worst case, the tree can be skewed, resulting in a space complexity of O(n), but in a balanced tree, the space complexity is O(log n).
Next Post Previous Post