Amazon Interview Question

Binary search tree find the second max

Interview Answers

Anonymous

Jul 9, 2012

yes

Anonymous

Aug 21, 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

Jul 9, 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