Maximum Depth / Tree Traversal
Problem
Compute the maximum depth of a tree — the number of nodes along the longest path from the root down to a leaf. The tree may be binary or n-ary (e.g. a nested issue / sub-issue structure).
Input / Output
- Input: the root of the tree (each node holds its children).
- Output: an integer, the maximum depth (0 for an empty tree, 1 for a single root).
Constraints
- Up to 10^4 nodes.
- The tree may be badly skewed (a chain), so a recursive solution risks deep recursion.
Example
A
/ \
B C
|
D
max depth = 3 (A -> C -> D)
added …