DSA Patterns (Multi-Lang)•Part 7: Binary Trees, BSTs & Traversals (53 - 64)
Maximum Depth of Binary Tree.
In Simple Words:
Height of a tree is 1 + max(depth(left), depth(right)). Base case: empty node returns 0.
- Time: $O(N)$ | Space: $O(H)$
Multi-Language SolutionsPython 3 • JavaScript • TypeScript • Java • C++
Switch tabs or choose Alldef maxDepth(root: TreeNode | None) -> int:
if not root: return 0
return 1 + max(maxDepth(root.left), maxDepth(root.right))