How Deep the Cave Goes
A caving club has mapped a cave as a binary tree: from each chamber at most two tunnels lead further down, and tunnels never rejoin. Each chamber has a number painted on the wall. To know how many rope anchors to pack, the club needs the number of chambers on the longest descent, counting the entrance and the last chamber.
Your task
Write caveDepth(root). Return the number of nodes on the longest path from the root down to a leaf. An empty tree has depth 0, and a tree with only a root has depth 1.
Trees are shown in level order, like [1, 2, 3, null, 4], where null marks a missing child. Your function receives real TreeNode objects with val, left and right.
caveDepth(root: TreeNode) → int
Examples
root = [1, 2, 3, null, 4, 5, null, null, null, 6]
4
The left descent 1 → 2 → 4 passes through 3 chambers, and the right one, 1 → 3 → 5 → 6, passes through 4. Following only the left tunnels would wrongly give 3.
root = [8]
1
The cave is only the entrance. The answer counts chambers and not tunnels, so it is 1 and not 0.
Constraints
- 0 ≤ number of chambers ≤ 1,000
- -1,000 ≤ chamber number ≤ 1,000
- The tree is at most 500 levels deep.
Start with brute force
List every route from the entrance to a dead end and keep the longest. Storing the routes means copying paths over and over, and a chain of 1,000 chambers makes routes up to 1,000 long. You only need lengths, and each chamber can compute its depth from its children's answers in a single visit.
Hints
0 of 4 shown- Hint 1
Stand in chamber 3 of the first example and ignore everything above. How deep is the cave below you, counting chamber 3?
- Hint 2
The depth from any chamber is 1 (itself) plus the depth of its deeper sub-cave. You only know which is deeper after measuring both.
- Hint 3
That definition calls itself on smaller trees. What is the answer for an empty tree, which is a tunnel that leads nowhere?
- Hint 4
If the node is null, return 0. Otherwise return
1 + max(depth(left), depth(right)).
Walkthrough and solution
Unlocks when you pass every test. Stuck for a while? Looking is fine too, as long as you then close it and write the code yourself.
How to solve it
Every chamber is the entrance of a smaller cave, so the depth of a cave is one chamber plus the depth of its deeper sub-cave, and an empty tunnel has depth 0.
Trace [1, 2, 3, null, 4, 5, null, null, null, 6], bottom up:
- Chambers 4 and 6 have nothing below them, so each has depth
1 + max(0, 0) = 1. - Chamber 5 has only chamber 6 below it, so its depth is
1 + max(1, 0) = 2. - Chamber 2 has only chamber 4 below it, on the right, so its depth is
1 + max(0, 1) = 2. - Chamber 3 has only chamber 5 below it, so its depth is
1 + max(2, 0) = 3. - The entrance, chamber 1, has depth
1 + max(2, 3) = 4.
Each chamber is visited once and returns a single number to its parent.
function caveDepth(root) {
if (root === null) return 0; // no chamber, no depth
const left = caveDepth(root.left);
const right = caveDepth(root.right);
return 1 + Math.max(left, right);
}def cave_depth(root):
if root is None: # no chamber, no depth
return 0
left = cave_depth(root.left)
right = cave_depth(root.right)
return 1 + max(left, right)Run your code to see results here. Run checks the examples. Submit checks every test, including hidden edge cases.