Amazon interview question

Binary search tree find the second max

Interview Answers

Anonymous

9 July 2012

yes

Anonymous

21 Aug 2012

Second max value is simply the value of the second last node in an in-order traversal of the BST(assuming unique node values)

Anonymous

9 July 2012

In a binary search tree all the nodes in left sub-tree have smaller or equal key than the parent's key and all the nodes in right sub tree have greater key than the parent node's key. class BST{ ..... ..... public int getSecondMaxKey(){ BST lastNode = null; BST secondLastNode = null; BST next = null; while(this.rightChild != null){ secondLastNode = lastNode; lastNode = this; this = this.rightChild; } return secondLastNode; } }

1