Amazon Interview Question

Write a program that takes an integer and gives the closest node in a binary tree.

Interview Answers

Anonymous

Mar 15, 2012

Using a normal binary tree search but keep tracks of the node that has the closest value. Node find(Node n, Value v){ int gDiff = infinite; Node cNode; while( n != null){ int diff = Math.abs(n.value-v); if(diff v){ n = n.left; } }//end while return cN; }end method

1

Anonymous

Jan 3, 2012

If we take abs(x - node.value) then the problem becomes find the min value in binary tree.