Return the maximum depth of the binary tree.
[3,9,20,null,null,15,7] → 3
# --- 2. Maximum Depth of Binary Tree ---
# Problem: Return the maximum depth of the binary tree.
# Input: [3,9,20,null,null,15,7]
# Output: 3
# Time Complexity: O(n)
def self.max_depth(root)
return 0 if root.nil?
left = max_depth(root.left)
right = max_depth(root.right)
[ left, right ].max + 1
O(n)
O(h)