Question d’entretien chez Ooyala

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.

Réponses aux questions d'entretien

Utilisateur anonyme

30 mars 2014

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

Utilisateur anonyme

30 déc. 2010

Perfect syntax was not required