CJCoding With Joseph
15free / daySign inCreate account

Find How Deep a Value Sits

Complete depthOf(int value) so it returns how deep value sits in the tree, or -1 when value is not in the tree.

Reminder: depth versus height. The depth of a node is how many links you follow from the root to reach it: the root is at depth 0, its children at depth 1, their children at depth 2, and so on. Height measures a whole tree from the top down, while depth measures one node from the root.

depth 0         50
               /  \
depth 1      30    70
            /  \     \
depth 2   20    40    80

Reaching a node in a BST takes exactly one path from the root, the same path contains() already walks. Count every step down along the way and that count is the depth.

Requirements: 1. Start at root with a depth of 0. 2. When the current node holds value, return the current depth. 3. Each time you move down to a child, add 1 to the depth. 4. Return -1 when the walk falls off the bottom of the tree without finding value. 5. Do not change the tree.

The Node class is provided - do not rename it and do not add public to it, because a Java source file may contain only one public class.

Do NOT write a main method.

Expected Output:

0
1
2
2
-1
3

= newline. The output ends with a newline — print one after the last line.

Hints3 available
Topics:
Data Structures & AlgorithmsBinary Search Trees
1
Tab to indent (Esc then Tab to move on) · Ctrl+Enter to run · Shift+Alt+F to format · Ctrl+Space to expand shortcuts (sysout, psvm, fori)

Your Output

Run your code to see the output here...

Test Cases

Run your code to see test case results.