Find How Deep a Value Sits
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 80Reaching 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.
Your Output
Run your code to see the output here...