Google Interview Question for Software Engineer in Tests






Comment hidden because of low score. Click to expand.
4
of 4 vote

boolean isMirrorImage(node n1,node n2)
{
	if(n1==null && n2==null)
		return true;
	else if(n1==null || n2==null)		
		return false;
	else if(n1.val != n2.val)		
		return false;
	
	return isMirrorImage(n1.left,n2.right) && isMirrorImage(n1.right,n2.left);
}

- gulusworld1989 October 17, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

i think the question was referring to a tree being a mirror to itself?

- Anonymous October 19, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

it's the same thing
In that case u can call the above function as
isMirror(n,n)
where n is the root of the required node

- gulusworld1989 October 19, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

cool sln, thnx

- erm October 21, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

boolean isMirrorImage(node n1,node n2)
{
	if(n1==null && n2==null)
		return true;
	else if(n1==null || n2==null)		
		return false;
	else if(n1.val != n2.val)		
		return false;
	// it should also chekc the node's values, which was actually missing!
	return isMirrorImage(n1.left,n2.right) && isMirrorImage(n1.right,n2.left) && (n1.value==n2.value);
}

- guest August 06, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Dear "guest"

Are you an actual idiot or can you not read through the code.
His code was fine.

// it should also chekc the node's values, which was actually missing!


As far as this comment goes, the two statements that is

if(n1.val ! = n2.val)
	return false;

check precisely this point.

- Anonymous March 11, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

hello, can anybody explain what does mirror image of tree means?? please give example. I am not aware of this term in tree.

- Anonymous October 19, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Symmetric wrt the vertical line going through the root.

- memo October 19, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Use any traversal algorithm, e.g. inorder, to traverse the tree. Now traverse the tree using the same algorithm except that whenever we follow node.left in the original algorithm we follow node.right instead. Perform both traversal in lock-step on the tree and at each step check to see if both traversals are making exact opposite turns (e.g. if traversal 1 makes a node.left turn, traversal2 makes a node.right turn) If there is one mismatched turn then return false else return true.

static string Traversal1(Node c)
{
string retVal = string.Empty;
if (c.left != null)
{
retVal = retVal + "l";
retVal = retVal + Traversal1(c.left);
}
if (c.right != null)
{
retVal = retVal + "r";
retVal = retVal + Traversal1(c.right);
}
return retVal;
}

static string Traversal2(Node c)
{
string retVal = string.Empty;
if (c.right != null)
{
retVal = retVal + "r";
retVal = retVal + Traversal2(c.right);
}
if (c.left != null)
{
retVal = retVal + "l";
retVal = retVal + Traversal2(c.left);
}
return retVal;
}

static bool IsMirrorImage(Node root)
{
string s1 = Traversal1(root);
string s2 = Traversal2(root);
s2.Replace("l", "x");
s2.Replace("r", "l");
s2.Replace("x", "r");
if (string.Compare(s1, s2) == 0)
{
return true;
}
else
{
return false;
}
}

- Anonymous October 19, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Take INORDER traversals of both the binary trees and make 2 strings out of them.
Reverse one string and do a string match on the other. If they match then the 2 binary trees are mirror images of each other.
Tried this with few scenarios and it works.

- Nikhil Limaye November 09, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This would not work.

1
|\
2 4
|\  \
4 5  2
      \
       5

- vrc November 11, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

actually, it should be

1
|\
2 5
|\  \
4 5  2
      \
       4

- vrc November 11, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Why wouldn't this work? Please post your comment with an example of a tree, its mirror and INORDER traversals under a case where this doesnt work.

- limaye.nikhil November 29, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

TreeA
...1...
..2....
.3.4...
.......

TreeB
...3...
..2....
.4.....
1......

- ace April 27, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Sorry, the solution I went with took two root nodes and determined if they are mirror images of each other, which would not work in the above example if using inorder traversals.

Inorder traversal won't work in the example below:

1
   /   \
  2     3
 /     / 
3     2

- ace April 27, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Awesome solution Nikhil, thanks.

- Badal November 03, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

INORDER travel, to get the path string,
e.g. a mirror B-Tree
1
/ \
2 2
/\ /\
3 4 4 3
the INORDER path is 3241423, it is easy to judge whether the path is mirror(compare A[i] and A[N-i])

- vvvvv December 24, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Mostly the same as soln by gulusworld1989

boolean isMirrorOfItself(Node r)
{
     if ( r ==null ) {
          return true;
     }

    return isMirror(r.left,  r.right);
}

boolean isMirror(Node l, Node r)
{
          // both are null
    if (l==null && r == null) {
                    return true;

}

// only one is null
    else if (r ==null || l ==null || r.value != l.value) {
                   return false;
    }
           return isMirror(l.left, r.right)  && isMirror(l.right, r.left);
}

- Krishna January 08, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

If the depth nodes are palindormes then the BTree is a mirror tree.
Recursively find the depth nodes and see if its a palindrome.

- Anon January 13, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

If the depth nodes are palindormes then the BTree is a mirror tree.
Recursively find the depth nodes and see if its a palindrome.

- Anon January 13, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Do Simple DFS and check at each righ & left childs

public boolean checkMirror(Node root)
{
	List<Node> queue = new LinkedList<Node>();	
	queue.add(root);
	
	while(queue.isEmpty() == false)
	{
		Node n = queue.remove();
		Node l = n.left();
		Node r = n.right();
		
		if((l == null && r != null) ||
				(l != null && r == null))
			return false;
			
		if(l != null && r != null)
		{
			if( l.val() != r.val()) return false;
			queue.add(l);
			queue.add(r);
		}		
	}
	return true;
}

- arindam December 03, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

sorry for the typos & grammer .. left & right childs ??? haha.. should have been child.. :)

- Anonymous December 03, 2013 | Flag


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More