Pergunta de entrevista da empresa Google

Find second largest number in a BST. Find peak element in an array using O(logn) method.

Resposta da entrevista

Sigiloso

14 de mar. de 2017

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; }