This function returns distance of x from root . This function assumes that x exists in BST and BST is not NULL .
function distanceFromRoot ( root , x ) { if ( root . key == x ) return 0 ; else if ( root . key > x ) return 1 + distanceFromRoot ( root . left , x ) ; return 1 + distanceFromRoot ( root . right , x ) ; }