Maximum Depth of Binary Tree

 

  1. Clarify the problem:

    • The problem requires finding the maximum depth of a binary tree.
    • We need to implement a function that takes the root of a binary tree as input and returns the maximum depth of the tree.
  2. Analyze the problem:

    • Input: The root node of a binary tree.
    • Output: An integer representing the maximum depth of the binary tree.
    • Constraints:
      • The binary tree can be empty (null), in which case the maximum depth is 0.
      • The binary tree can have multiple levels.
  3. Design an algorithm:

    • We can solve this problem using a recursive approach.
    • If the root is null, we return 0 since there are no nodes in the tree.
    • Otherwise, we recursively calculate the maximum depth of the left and right subtrees.
    • The maximum depth of the entire tree is the maximum depth of the subtrees plus 1.
  4. Explain your approach:

    • We will implement a function called maxDepth that takes the root of a binary tree as input.
    • If the root is null, we return 0.
    • Otherwise, we recursively calculate the maximum depth of the left and right subtrees.
    • We return the maximum of the depths of the subtrees plus 1.
  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 maxDepth(root): if root is None: return 0 left_depth = maxDepth(root.left) right_depth = maxDepth(root.right) return max(left_depth, right_depth) + 1
  7. Test your code:

    python
  8. # Test case 1 # Input: # 3 # / \ # 9 20 # / \ # 15 7 # The binary tree has a maximum depth of 3. # Expected output: 3 root = TreeNode(3) root.left = TreeNode(9) root.right = TreeNode(20) root.right.left = TreeNode(15) root.right.right = TreeNode(7) assert maxDepth(root) == 3 # Test case 2 # Input: null # An empty tree has a maximum depth of 0. # Expected output: 0 assert maxDepth(None) == 0
  9. Optimize if necessary:

    • The current solution has a time complexity of O(n), where n is the number of nodes in the binary tree.
    • This is because we visit each node once during the depth-first search.
    • The space complexity is O(h), where h is the height of the binary tree.
    • This is because the maximum depth of the recursion stack is equal to the height of the tree.
  10. Handle error cases:

    • The code assumes that the input is a valid binary tree.
    • It does not handle invalid input or error cases, such as non-tree inputs.
    • In such cases, the behavior of the code is undefined.
  11. Discuss complexity analysis:

    • The time complexity of the solution is O(n), where n is the number of nodes in the binary tree.
    • The space complexity is O(h), where h is the height of the binary tree.
    • The best-case scenario occurs when the binary tree is empty, and the function returns 0 in constant time.
    • The worst-case scenario occurs when the binary tree is a skewed tree, and the function traverses all n nodes, resulting in O(n) time complexity.
    • The average-case time complexity is also O(n) for random inputs.
    • The space complexity depends on the height of the binary tree, and it can be O(n) in the worst case for a skewed tree.
Next Post Previous Post