Google Interview Question
Software Engineer / DevelopersCountry: United States
I have come up with a solution for finding the in order successor when the links to parents are not provided. Could you check if this is right? I already checked. I would feel confident if someone cross checks it.
public Node inOrder(Node n, Node root){
if(root == null){
return null;
}
Node successor;
Node max;
while(root != null && root != n.data){
Node parent = root; // 4 // 6
if(root.left != null && n.data <= root){
if(root >= max){
max = root; // 6
}
root = root.left; //5
successor = parent; //6
}else if(root.right != null && n.data >= root){
if(root >= max){
max = root; //
}
root = root.right; //
successor = parent; //
}
}
if(root.right == null && root == parent.left){
return max;
}
if(root.right != null){
return leftMostChild(root);
}
}
public Node leftMostChild(Node n){
Node current;
if(n.right != null){
current = n.right;
}
while(current.left != null){
current = current.left;
}
return current;
}
This should do the job (or the bugless version of it)....
void succ(Node x, int key)
{
static bool found;
if(x==nil) return;
succ(x.left);
if(x.val == key) found=true;
else if(found) printf(x.val or x), found=false, return;
succ(x.right);
}
void succ(Node *x, int key)
{
static bool found;
if(x==NULL) return ;
succ(x->left);
if(x->val == key) found=true;
else if(found) cout<<x.val or x<< "is successor", found=false, return;
succ(x->right);
}
In search trees, you can cut the complexity by following the actual path down, noting the last "possible" successor ancestor, then try diving below the target node to find a successor, else use the ancestor.... (works on paper)... but blah blah blahhhhhhhhhhhhhhhhhhh that's boring.
@algos interpretation of the problem is very smart. However, if this question is concern with the actual structure of the given BST, then we can use something like the code below:
struct bst* InorderSuccessor(struct bst *b, struct bst *value)
{
if(tmp->lc!=NULL)
return tmp->lc;
else if(tmp->rc!=NULL)
return tmp->rc;
else if(tmp==tmp->parent->lc && tmp->parent->rc!=NULL)
return tmp->parent->rc;
else
{
struct bst *tmp1=tmp->parent;
while(tmp1->parent!=NULL && (tmp1->rc==NULL || tmp1->rc==tmp))
{
tmp=tmp1;
tmp1=tmp1->parent;
}
if(tmp1->rc!=NULL)
val=tmp1->rc;
}
return NULL;
}
No in order successor to 6, and here is my solution.
public class BSTinorder
{
// Return the in order successor of a given node in a BTS
public class Node {
public Node right;
public Node left;
public float data;
public Node(float data, Node left, Node right) {
this.right = right;
this.left = left;
this.data = data;
}
}
private Node root = new Node(4,
new Node(2,
new Node(1, null, null),
new Node(3, null, null)
),
new Node(6,
new Node(6,
new Node(5,
new Node(4.5f, null, null),
new Node(5.5f, null, null)
),
null
),
null
)
);
public Node after(float value, Node node) {
if (value >= node.data) {
return node.right == null ? null : after(value, node.right);
} else {
if (node.left == null) {
return node;
}
Node afterLeft = after(value, node.left);
if (afterLeft == null) {
return node;
}
return node.data < afterLeft.data ? node : afterLeft;
}
}
public static void main(String[] args)
{
BSTinorder instance = new BSTinorder();
Node node = instance.after(Float.parseFloat(args[0]),instance.root);
if (node == null) {
System.out.println("no successor");
} else {
System.out.println(node.data);
}
}
}
The logic that I would put into this question would be sth like this:
if(root)
{
InOrderSuccessor(r->left)
}
The logic that I would put into this question would be sth like this:
Node * InOrderSuccessor (Node * r, int data)
{
if(root)
{
InOrderSuccessor(r->left, data)
if(r->data > data)
{
return r;
}
else
{
InOrderSuccessor(r->right, data);
}
}
}
why don't we just do in order traversal of the tree and store it into an array, then we just simply find the next item after the node we are interested. If null then means it doesn't have an in order successor. time O(n), space O(n). I guess the only penalty here is the extra memory space used.
In order successor in a BST will be the smallest number greater than the given number.
Which would be the left most node in its right subtree.
def in_order_successor(node):
if node.right is None:
return None
temp_node = node.right
while temp_node.left != None:
temp_node = temp_node.left
return temp_node
as per the sequence the possible in-order successor could be 7. if you need to find the in-order successor with the existing data then return -1 as that doesn't exist
---------------4
-------2--------------6
----1----3-------5-------7
--------------4.5--5.5
4 is average of 2&6
2 is average of 1&3
5 is average of 4.5&5.5
6 is average of 5&7
There is no 7 in the BST. 6 has just left child and no right child. Now what would be the answer?
There is no in-order successor for the node with value 6.
- Jayasraj October 22, 2013