Interview Question

Software Engineering Interview Mountain View, CA

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.
Answer

Interview Answer

2 Answers

0

Perfect syntax was not required

Interview Candidate on Dec 30, 2010
0

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));
    }

Mario R. on Mar 30, 2014

Add Answers or Comments

To comment on this, Sign In or Sign Up.