Find second largest number in a BST. Find peak element in an array using O(logn) method.
Anonimo
Node secondLargest(Node root) { if(root==null) return null; // traverse the right subtree if present prev = null; while(root.right != null) { prev = root; root = root.right; } if(prev==null) { // right subtree is not present // return rightmost node in left subtree of the root root = root.left; prev = root; while(root!=null && root.right != null) { prev = root; root = root.right; } } return prev; }