Dsa
Trees
Warm-up
Find Maximum Depth
Given the root of a binary tree, find its maximum depth. The maximum depth is the number of nodes along the longest path from the root down to the farthest leaf node.
Input Format:
- The input starts with an integer n, the number of nodes in the tree.
- The following n lines contain the values of the node followed by its left and right child (or -1 if there is no child).
Output Format:
- A single integer representing the maximum depth of the binary tree.
Example Input:
3
1 2 3
2 4 -1
3 -1 -1
4 -1 -1
Example Output:
3
Key concepts
treesdepthrecursion
Practise this out loud — free
Start a mock interview on THIS exact question — a voice AI interviewer opens with it, pushes back like a real onsite, then hands you an instant scorecard.
🎙 Practise this question now