Amazon Interview Question
Software Engineer / DevelopersYour code will not work for the case where the tree is a left skewed BST, with the largest element as the root.
So you have to replace the first three lines of your function with
...
if(!root){
return NULL;
}
if(!root->right){
if(!root->left){
return NULL;
}
else{
return root->left;
}
}
...
Just do an inorder traversal and grab the 2nd to last.
void inorderTraverse( Node* root, vector<Node*>& nodes )
{
if ( !root )
return;
inorderTraverse( root->left, nodes );
nodes.push_back( root );
inorderTraverse( root->right, nodes );
}
Node* getSecondLargestNode( Node* root )
{
vector<Node*> inorderTraversal;
inorderTraverse( root, inorderTraversal );
if ( inorderTraversal.size() < 2 )
return NULL;
else
return inorderTraversal[inorderTraversal.size() - 2];
}
First code post doesn't account for this case:
10
/
5
\
8
Second post is good, but requires whole tree traversal, with memory storage n.
How about this:
Node * secondLargest(Node* root)
{
if (!root)
return NULL;
else if (!root->right)
return maxValue(root->left);
else if (!root->right->right)
return root;
else
return secondLargest(root->right);
}
where maxValue is a function that returns the right most node.
node* getSecondHighestNode( node* root)
{
if(!root)
return NULL;
int max;
node* cur = root;
while(cur->right)
{
cur = cur->right;
}
max = cur->data;
node* secondMax = NULL;
cur = root;
while(cur)
{
if(cur->data < max)
{
secondMax = cur;
cur = cur-> right;
}
else
cur = cur-> left;
}
return secondMax;
}
why the hell people are wasting tiem for such silly quesitons....as some one already mentioend do a simple DFS/BFS and pick the penultimate node and u are done = O(n)
else do it this way...
pick the max and delete it = O(logn) + O(1)
now pick the max again = O(logn)
Total run time = O(logn)
as the quesiton dint say that we're not allowed to modify the tree, the above solution should do the job...and runs in O(logn)..thats good enough~!
Two cases :
1. Parent node is the largest node.
Parent node can be either a normal parent or a root.
return its immediate left child as the SecondLargest.
2. Right most is the largest node.
return its parent as the SecondLargest.
code:
=====
void SecondLargest(node *root, int *secondLargest)
{
if(!root)
return;
if(!SecondLargest(root->right))
{
if(!secondLargest)
{
if(!root->right->left)
*secondLargest = root->right->left->value; //if the parent is the largest then its immediate left is the second largest
else
*secondLargest = root->value; //if the rightmost child is the largest then its parent is the secondlargest
}
}
if(!secondLargest) //if root is the largest node (ie)left oriented tree
{
if(!root->left)
*secondLargest = (root->left->value);
else
return; //tree with only root node
}
return;
}
test case : no node , 1 node
step 1 : from root keep moving right till null ... got the max element
step 2 :
case 1: if left subtree(right subtree not possible) .. go to its root and
repeat step 1
case 2: if there is no left subtree then its parent (max node will always
be the right child of parent)
Node pick = TreeTop;
Node parent = null;
while (pick.right != null)
{
parent = pick;
pick = pick.right;
}
if parent == null
{
pick = pick.left;
while (pick.right != null)
{
pick = pick.right;
}
return pick;
}
else
{
if pick.left == null
{
return parent;
}
else
{
pick = pick.left;
while (pick.right != null)
{
pick = pick.right;
}
}
}
- Anonymous July 30, 2009