## Saturday, December 19, 2015

### Find the next neighbor at the same level for a given node. | CODING INTERVIEW ARCHIVES

Find the next neighbor at the same level for a given node. | CODING INTERVIEW ARCHIVES
For a given binary tree with node structure as shown:

struct TreeNode{
int data;
TreeNode* left;
TreeNode* right;

TreeNode* parent;
};

Output the node that comes next to it at the same level. In case there is no such node output -1.
The basic idea is that since for any given traversal we will always visit the given node before we visit it's neighbor, we can maintain some kind of a global or static variable that keeps track of the level of our node. Once we have that we can simply print the node that comes next at the same level during our traversal.

Now to keep track of the levels of various nodes we need to pass a variable showing the level we are currently in.

Here's the code for the problem:

We can use any traversal as long as the left subtree is explored before the right subtree. The code shown here uses inorder traversal. The main function called contains only the pointer to the node.
So before we start our traversal we need to find the root. This can be done easily since we have the parent pointer for each node. We can use something like this to find root:

TreeNode* getRoot(node* ptr){
TreeNode* root=ptr;
while(root->parent){
root=root->parent;
}
return root;
}

Here's the function that is called from our main, and along with it the utility function that does the actual computation for us.

TreeNode* findNextAtSameLevel(TreeNode* ptr){
TreeNode* root=getRoot(ptr);
findNextAtSameLevelUtil(root,ptr,0);
}

void findNextAtSameLevelUtil(node* root,node* ptr,int curr){

//Flag to keep track if ptr has been found in the tree
static bool flag=false;

//Stores the level of ptr in the tree
static int level;

if(root){

if(root->left){
findNextAtSameLevelUtil(root->left,ptr,curr+1);
}

//found ptr, now update level and toggle flag to true
if(root==ptr && flag==false){
level=curr;
flag=true;
}

//found the node we were looking for
if(curr==level && flag==true && root!=ptr){
flag=false;
}

if(root->right){
findNextAtSameLevelUtil(root->right,ptr,curr+1);
}
}
}

The variable answer being returned in the original function can be declared globally. An important point to keep in mind here is that when we find our answer we need to set the flag to false again. If we don't do this the function will keep updating the answer and will ultimately store the rightmost node at that level.
Approach 2:

This is a slightly cleaner approach as it does not involve finding the root. Here's the algorithm:
• Check for the base cases
• if node is NULL then return NULL
• if node -> parent is NULL then return NULL
• if node is left child and parent has a right child too, then return node -> parent -> right.
• Call the neighbour function for node's parent.
• If the parent's neighbour doesn't have any child, find it's neighbour.
• This will be repeated until we find a node that has a child, or the node has no neighbour
We can imagine this as applying level order traversal for the level of node's parent, recursively checking for all nodes in the penultimate level.

Here's the implementation:

TreeNode* next_neighbour(struct node* root){

//Check for base cases
if(root == NULL)
return NULL;

if(root->parent == NULL)
return NULL;

if(root == root->parent->left && root->parent->right != NULL)
return root->parent->right;

//recurse until you find a neighbour with a child, or you run
//out of neighbours
TreeNode* parent_neighbour = next_neighbour(root->parent);
while(parent_neighbour){
if(parent_neighbour->left)
return parent_neighbour->left;
if(parent_neighbour->right)
return parent_neighbour->right;
parent_neighbour = next_neighbour(parent_neighbour);
}
return NULL;
}
Read full article from Find the next neighbor at the same level for a given node. | CODING INTERVIEW ARCHIVES