Amazon Interview Question
SDE1sCountry: India
Interview Type: Written Test
That's simply the inorder successor..either the leftmost child of the right child or right child or parent (in preference order, depending on existence).
Well of course it's the successor of in-order traversal - but it's not that simple to perform in-order traversal iteratively from an arbitrary node.
Your answer is wrong because the given node could be the right child of its parent, so the parent is not the next largest node (it would be a higher ancestor in this case).
why do we need a parent pointer just wondering. The next largest node will be the left most node of the right node of the given node.
This is why you need parent pointer
minRightChild = getMin(node -> right); //if node->right is null then return intmin
If node->data <= parent->data
return Max(minRightChild, parent->data)
else
return minRightChild
in case if a node has no childrens then its parent could be the next largest. One cannot wonder in the written test for a problem statement
The function 'findNextLargest' will do the job
node* findElement(node* r,int val)
{
if(!r)
return NULL;
if(r->data == val)
return r;
if(r->data < val)
return findElement(r->right,val);
else
return findElement(r->left,val);
}
node* findMin(node* s)
{
while(s->left)
s = s->left;
return s;
}
int findNextLargest(node* r,int val)
{
if(!r || !r->right)
{
cout<<"No such element"<<endl;
return ERROR_CONST;
}
node* s = findElement(r,val);
if(s->right != NULL)
{
node* t = findMin(s->right);
return t->data;
}
while(s != r)
s = s->parent;
if(s==r)
return ERROR_CONST;
return s->data;
}
int findLargest(node*root , int val) {
if (!root) //tree is empty
printf("\ntree is empty") ;
exit();
if (val == root->value) {
printf("\nOnly 1 element is present");
exit();
}
node * element ;
if (val < root->value)
element = findElement(root->left , value);
else
element = findElement(root->right , value);
if (element->right == NULL) {
if (element = element->parent->left) { // the element is a left child . So its parent is the //next largest
return parent;
}
//Now the element is a right child .
if(element->value > root->value) { //the right most child of the right subtree from the //root node is the largest element
printf("\nThis element is the largest in the tree");
exit();
}
while() { // Now have to traverse back through the parent of
//each node till we reach the parent of the subtree which is a left child. Now the parent //of this node will be the next largest.
if (element = node->parent->left) {
return parent->value;
}
element = element -> parent;
}
}
//Now the right child is not empty . So the next higher element will be the left-most child of the right-child .
element = element->right;
while(element->left != NULL) {
element = element->left;
}
return element->value;
}
findElement(node * root , int val) // This will do a tree traversal to find the right element.
1. If the node has right child. then return leftmost child of the right subtree.
2. Else if node is the left child of its parent then return its parent
3. Else go up to the parent until you find a node that is a left child of its parent. Return the parent.
4. Return null
Here is the code
- CodeNameEagle May 15, 2013