Amazon | Distance Between 2 Nodes in BST

Write a function that given a BST, it will return the distance (number of edges) between 2 nodes.

For example, given this tree

         5
        / \
       3   6
      / \   \
     2   4   7
    /         \
   1           8

The distance between 1 and 4 is 3: [1 -> 2 -> 3 -> 4]

The distance between 1 and 8 is 6: [1 -> 2 -> 3 -> 5 -> 6 -> 7 -> 8]

Comments (44)