Fix the height function
Standard · Find the bug · about 5 min
This function should return the number of nodes on the longest root-to-leaf path. It returns a wrong answer for every tree. Click the line with the bug.
Example 1
Input:
[[1,2,3]]
Output:
2
The buggy version returns 3.
Constraints
- 0 ≤ number of nodes ≤ 1,000
- -1,000 ≤ Node.val ≤ 1,000
- The tree is given in level order; null marks a missing child
Learn the idea first: Recursive problems on trees: height, size, mirror, paths, a visual lesson.
More Recursive problems on trees: height, size, mirror, paths problems
- Who reports first? Warm-up
- Complete the node counter Warm-up
- Max depth Standard
- Invert a tree Standard
- Path sum Standard
- Same tree Standard
- Diameter of a tree Challenge