Given an object hierarchy (like in JavaScript), write a function that returns the depth of an object in this tree which is passed to the function.
Anonymous
Depth of an element in a tree is just this: public static int depthOfX(SeraTree t, int x){ return depthOfX( t, x, 0); } public static int depthOfX(SeraTree t, int x, int level){ if ( t == null ) return -1; if (t.value == x) return level; level++; return Math.max(depthOfX(t.left,x,level), depthOfX(t.right,x,level)); }
Check out your Company Bowl for anonymous work chats.