Infosys Interview Question for Software Engineer / Developers

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

Use a stack.
Initialization: push the root in
Iterative steps :
1 )pop the top element
2 )Swap left <--> right ptrs.
3 )put them in stack
Repeat until stack is empty

- knap October 07, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
of 0 vote

The problem says to make a copy, not modify the tree.

Idea is to use two stacks. We do similar pushing and popping for both.

C# like code.

Tree Mirror (Tree root) {
    Stack <Tree> originalStack = new Stack<Tree>();
    Stack <Tree> mirrorStack = new Stack <Tree>();
    Tree mirrorRoot = new Tree();
    while (originalStack.IsNotEmpty()) {
        Tree node = originalStack.Pop();
        Tree mirrorNode = mirrorStack.Pop();
        mirrorNode.Left = new Tree(node.Right); // Assume this constructor copies data and IsLeafNode property
        mirrorNode.Right = new Tree(node.Left); 
        if (node.IsLeafNode()) {continue;} // No need to push.
    return mirrorRoot;

There might be bugs and usage of IsLeafNode is weird (I was too lazy to do null checks), but I hope this gives the basic idea.

- T March 15, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
of 0 vote

Actually using a Queue would be far easier. Do a BFS on the tree, collect all the nodes in a queue. Create a new instance of your BinaryTree class (or whatever it is) and while the queue is not empty, pop element from queue and insert into new BinaryTree...violaa!! you have a clone!

- Anonymous December 16, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
of 0 vote

The general algorithm can be given as follows:- (Assuming you have a BinaryTree class, which infact you should, following good OO)

1) binary tree can have a "collect" method that will return a list of all values from it, using BFS (it is very important that you use BFS as it preserves the order in which the nodes were created).

2) Create a new BinaryTree and insert all values from the list on at a time. The new tree will be exactly same as the previous one.

- Parth_Mehta_UIC December 16, 2011 | Flag Reply

Add a Comment

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


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


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