Amazon Interview Question
SDE-2sCountry: India
Interview Type: In-Person
keep track of previous visited node ......
void pre(struct node *t,struct node **p,struct node *s){
if(t!=NULL){
pre(t->left,p,s);
if(t==s ) {
printf("%d" ,(*p)->data);
}
*p=t;
pre(t->right,p,s);
}
}
Case 1: Node is left most node of BST.
Return NULL
Case 2: Node has left sub tree.
In this case it is Maximum Node in the Left Sub-Tree. i.e., the right most node in the left sub-tree
would be the in-order predecessor.
Case 3: Node has no left sub-tree.
In this case in-order predecessor will be the node where we took the latest right turn.
C++ version:
NODE * find_predecessor(NODE * root, NODE * node)
{
NODE * temp = root, *parent = NULL;
if (node->left != NULL){ // Max of the Left Sub-Tree
temp = node->left;
while (temp->right != NULL)
temp = temp->right;
return temp;
}
while ( temp != node ){ // Find the Ancestor where we took latest Right Turn
if (node->data < temp->data){
temp = temp->left;
}
else {
parent = temp;
temp = temp->right;
}
}
return parent;
}
Is this the right approach. I am keeping track of predecessor value using pre variable. Whenever I reach the given node, I return the tracked predecessor value.
public int inorderPredecessor(BST root, int x, int pre) {
if (root != null) {
pre = inorderPredecessor(root.left, x, pre);
if (root.value == x) {
System.out.println("Predecessor = " + pre);
return pre;
}
pre = inorderPredecessor(root.right, x, root.value);
}
return pre;
}
Java solution using DFS inorder traversal but using a Stack to keep track on predecessor, so space complexity could be bad here. Any other simpler, recursion-only solution is welcome :-)
/*
* j PREORDER: j f a d h o m
* / \ INORDER: a d f h j m o
* / \
* / \
* f o
* / \ / \
* a h m
* / \
* d
*
* Returns true if the item is found and its
* predecessor(null inclusive) found
*/
public boolean inorderPredecessor(Node node, T item, Stack<Node> stk) {
if (node == null)
return false;
if (inorderPredecessor(node.left, item, stk))
return true;
if (item.compareTo(node.item) == 0) {
T pred = stk.isEmpty() ? null : stk.pop().item;
System.out.printf("inorder-pred of %s is %s\n", item, pred);
return true;
} else {
stk.push(node);
}
if (inorderPredecessor(node.right, item, stk))
return true;
return false;
}
public void inorderPredecessor(Node root, T item) {
if (root == null) {
System.out.printf("InorderPredecessor: Tree empty");
return;
}
Stack<Node> stk = new Stack<Node>();
if (!inorderPredecessor(root, item, stk)) {
System.out.printf("inorder-pred of %s was not found\n", item);
}
}
Few test case outputs:
inorder-pred of j is h
inorder-pred of f is d
inorder-pred of o is m
inorder-pred of a is null
inorder-pred of h is f
inorder-pred of m is j
inorder-pred of d is a
inorder-pred of k was not found
inorder-pred of z was not found
Assuming we keep track of parent node:
public class Node<T> {
private Node<T> left;
private Node<T> right;
private Node<T> parent;
private T item;
public static Node inOrderPredecessor(Node node) {
if (node == null) {
return null;
}
if (node.left != null) {
return rightMostChild(node.left);
} else {
Node prev = node;
Node prevParent = prev.parent;
while (prevParent != null && prevParent.right != prev) {
prev = prevParent;
prevParent = prevParent.parent;
}
return prevParent;
}
}
public static Node rightMostChild(Node node) {
while (node.right != null) {
node = node.right;
}
return node;
}
}
If the left subtree is not null then the inorder predecessor will be the rightmost child of the left node of the given node whose predecssor has to be find and if it is null then the inorder predecessor can be find as follow :-
Travel up using the parent pointer until we see a node which is right child of it’s parent. The parent of such a node is the predecessor.
node * inOrderPredecessor( node *ptr)
{
if( ptr->left != NULL )
{
node *p=ptr->left;
while(p->right!=NULL)
p=p->right;
return p;
}
node *p = ptr->parent;
while(p != NULL && n == p->left)
{
ptr = p;
p = p->parent;
}
return p;
}
struct tnode *rightmost(struct tnode *root)
{
while(root->right!=NULL)
root = root->right;
return root;
}
struct tnode *getpre(struct tnode *root,struct tnode *node,struct tnode *prev)
{
if(root==NULL)
return NULL;
if(root==node)
{
if(root->left!=NULL)
return rightmost(root->left);
return prev;
}
struct tnode *temp=getpre(root->left,node,prev);
if(temp==NULL)
return getpre(root->right,node,root);
return temp;
}
Right most child of the left subtree of a given node is the inorder predecessor.
- khunima March 23, 2014