Microsoft Interview Question
Software Engineer in TestsMy first idea:
1. complete the rest of the tree with dummy nodes until it's a complete binary tree.
2. do a level order and save it to an array.
3. use the idea from binary heap to get the grand parent.
My second Idea :)
postorder(root,gparent,count){
if(root==null) return;
if(count==0){
root.granparent = gparent;
count=2;
}
postorder(root.left,root,count-1);
postorder(root.right,root,count-1);
}
call postorder(root,null,2);
@MaYanK , I think you are assigning parent instead of grand parent here .
Every time you are calling postorder(root,gparent,count) ->
1. either gparent is null
2. or gparent->left=root or gparent->right=root
so , gparent is not the grand parent here , its parent .
One more thing , it will only set the gparent for the nodes where cout = 0 .
so , suppose we are at node current_node ,
then postorder(current_node->left,..) and postorder(current_node->right,..) will be called with count = 1 , and so the gparent for those nodes will not be assigned .
We can do level order traversal keeping track of lastnode traversed and parentOfLastNodeTravesed.
before dequeuing the next level element enqueue NULL as indication of next level and parentOfLastNodeTravesed traversed.
void assign_gp(node * root, node * parent)
{
if(root)
{ if(root->left)
{ root->left->gparent=parent;
assign_gp(root->left,root);
}
if(root->right)
{ root->right->gparent=parent;
assign_gp(root->right,root);
}
}
}
void SetGrandParent(Node root) {
SetGrandParent(null, null, root);
}
void SetGrandParent(Node grandParent, Node parent, Node root) {
node.grandparent = grandparent;
if (root.left != null) {
SetGrandParent(parent, root, root.left);
}
if (root.right !=null) {
SetGrandParent(parent, root, root.right);
}
}
public static void AssignGrandParent(TreeNode root)
{
if (root == null)
return;
if (root.Parent != null)
root.GrandParent = root.Parent.Parent;
AssignGrandParent(root.LeftNode);
AssignGrandParent(root.RightNode);
}
Here is my C# implementation
static void SetGrandParent(Node n, Node parent, Node GrandParent)
{
if (GrandParent != null)
n.GrandParent = GrandParent;
if (parent != null)
{
if (n.Left != null)
SetGrandParent(n.Left, n, parent);
if (n.Right != null)
SetGrandParent(n.Right, n, parent);
}
else
{
if (n.Left != null)
SetGrandParent(n.Left, n, null);
if (n.Right != null)
SetGrandParent(n.Right, n, null);
}
}
and first Call Will be
SetGrandParent(tree, null, null);
I am sure next five lines could have been enough.
n.GrandParent = GrandParent;
if (n.Left != null)
SetGrandParent(n.Left, n, parent);
if (n.Right != null)
SetGrandParent(n.Right, n, parent);
main(){
assigngrandparent(root->left,root);
assigngrandparent(root->right,root);
}
assigngrandparent(node n,node gpn)
{
if(gpn!=null)
{node->left->gp=gpn; // if there is a left child for node
node->right->gp=gpn; // if there is a right child for node
}
assigngrandparent(node->left,node);// if there is a left child for node
assigngrandparent(node->right,node);// if there is a right child for node
}
Hi, Simplest approach...
Push(root)
while(isempty() == FALSE)
{
node = pop();
if(node != NULL)
{
if (node->left != NULL)
{
node->left->garandparent = node;
push(node->left)
}
else
push (NULL);
if (node->right != NULL)
{
node->right->garandparent = node;
push(node->right);
}
else
push (NULL);
}
}
And there u go dude...
class TreeNode
{
public int Data { get; set; }
public TreeNode Left { get; set; }
public TreeNode Right { get; set; }
public TreeNode GrandParent { get; set; }
public TreeNode(int data)
{
this.Data = data;
this.Left = null;
this.Right = null;
this.GrandParent = null;
}
public void SetGrandParent(TreeNode root)
{
Queue<TreeNode> q = new Queue<TreeNode>();
q.Enqueue(root);
TreeNode current = null;
while (q.Count > 0)
{
current = q.Dequeue();
if (current.Left != null)
{
q.Enqueue(current.Left);
CheckChildren(current.Left, current);
}
if (current.Right != null)
{
q.Enqueue(current.Right);
CheckChildren(current.Right, current);
}
}
}
private void CheckChildren(TreeNode parent, TreeNode grandParent)
{
if (parent.Left != null)
{
parent.Left.GrandParent = grandParent;
}
if (parent.Right != null)
{
parent.Right.GrandParent = grandParent;
}
}
public void DisplayFamily(TreeNode node)
{
if (node == null)
{
return;
}
DispayFamily(node.Left);
Console.WriteLine(" Grand father of {0} is : {1}", node.Data, (node.GrandParent == null) ? "Null" : node.GrandParent.Data.ToString());
DispayFamily(node.Right);
}
}
// My code
- Frodo Baggins August 13, 2010