Binary Trees — Recursion Native
Answer for a node from left + right children.
When to use this
- Depth
- Invert
- Same / symmetric
C++ template
int maxDepth(TreeNode* root) {
if (!root) return 0;
return 1 + max(maxDepth(root->left), maxDepth(root->right));
}Dry run
Root with deeper right child → depth = 1 + max(L,R).
Pitfalls
- Null base case
- Swap order
- BFS/DFS confusion
Say this in the interview
Null base, combine children.
Problems in this topic (3)
Each opens teach-first (animated when available), then the code panel.