352. How Deep Is a Value
Print the depth of the node holding value x, counting the root as depth 1.
Print -1 if no node holds that value.
Values are distinct, so there is at most one answer.
Carry a depth down as you walk, and stop when you find it. A -1 for absent is
a deliberate choice: it is a value no real depth can take, so the caller can
always tell the two cases apart.
Constraints - `1 ≤ n ≤ 100000` - `-1000000000 ≤ value, x ≤ 1000000000` - All values are distinct. - The input always forms a valid tree rooted at node 1.
Input
The first line contains two integers n and x.
The second line contains n values, where node i holds the ith value.
Each of the next n lines contains the left and right child of node i,
using 0 for no child. Node 1 is the root at depth 1.
Output
Print the depth of the node holding x, or -1 if there is none.
Hints
Four rungs, in order. The last two open once you have submitted an attempt. A wrong one counts.