Amazon Interview Question for Developer Program Engineers






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

private static BinaryTreeNode convertToMirror(BinaryTreeNode root) {
		BinaryTreeNode temp;
		if (root != null) {
			convertToMirror(root.getLeft());
			convertToMirror(root.getRight());

			temp = root.getLeft();
			root.setLeft(root.getRight());
			root.setRight(temp);
		}
		return root;
	}

- Vir Pratap Uttam May 04, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

to get the mirror image of the binary tree we have to just traverse the tree in post
oreder and in a bottom up fashion we have to swap the right son to left
code :
void MirrorImage(struct node *R)
{
struct node *temp=NULL;
if(R==NULL||(R->left==NULL &&R->right==NULL))
return;
MirrorImage(R->left);
MirrorImage(R->right);
temp=R->left;
R->left=R->right;
R->right=temp;
}

- geeks July 25, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Why is there a need to do it post order? We can swap the children and then call the function recursively on both the children, am I right? In the above written code, the function is called on children before they are swapped

- Aman July 29, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

absolutely, should work either way.
Can get more challenging, if a condition saying no recursion is specified.

- anon July 31, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Actually the order of traversal is not important -- the point is to continuously swap the left child and the right child nodes. Levelorder, preorder, postorder; they all work. Even inorder actually works; although I would not recommend it (it's not logically elegant).

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

public void Node mirror(Node b){
if(b!=null){
mirror(b.left);
mirror(b.right);

Node temp = b.right;
b.right = b.left;
b.left = b.right;
}






return b;
}

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

what in case we have just one child. you need to handle that case also...

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

Here is a crisp C++ function that mirrors a binary tree.

struct Node{
    int data;
    Node *left;
    Node *right;
} *root;

Node* mirror(Node *node)
{
    if(node==NULL)
        return NULL;
    else{
        Node *leftSubtree = node->left;
        Node *rightSubtree = node->right;
        node->left = mirror(rightSubtree);
        node->right = mirror(leftSubtree);
        return node;
    }
}

The invocation statement will be something like :

root = mirror(root);

- puppet_master October 06, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

//here is a solution without recursion using queues
q.enqueue(head);
mirror()
{
       while(q.isNotEmpty())
        {
                 node elem = q.dequeue();
                 if(elem.left==null && elem.right==null)
                             continue;
                 else
                 {
                            swap(elem.left, elem.right);
                            if(elem.left!=null) q.enqueue(elem.left);
                            if(elem.right!=null) q.enqueue(elem.right);
                 }

        }
}

swap(node elem1, node elem2)
{
node temp = elem1;
elem1 = elem2;
elem2 = temp;
}

- kavin January 19, 2012 | Flag Reply


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