Amazon Interview Question for Interns


Country: United States




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

struct node *mirror(struct node *root)
	{
		if(root==NULL)
			return;
		mirror(root->left);
		mirror(root->right);
		struct node *temp;
		temp=root->left;
		root->left=root->right;
		root->right=temp;
	}

- Kabeer August 10, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
2
of 2 vote

Traverse the tree in pre-order and swap left and right child.

public static void mirrorTree(TreeNode node){
if(node==null)return;
TreeNode temp=node.left;
node.left=node.right;
node.right=temp;
mirrorTree(node.left);
mirrorTree(node.right);


}

- OTR August 10, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

TreeNode* mirrorBT(TreeNode* root)
	{
		if(root == NULL)	return NULL;
		TreeNode *newRoot = new TreeNode(root->val);
		newRoot->left = mirrorBT(root->right);
		newRoot->right = mirrorBT(root->left);
		return newRoot;
	}

- Jason October 16, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

a non-recursive way:
template <typename T>
void BinaryTree<T>::mirror() {
    queue<BinaryTreeNode<T> *> q;
    if (root) {
        q.push(root);
        while (!q.empty()) {
            BinaryTreeNode<T> *front = q.front();
            q.pop();
            BinaryTreeNode<T> *temp = front->getLeftChild();
            front->setLeftChild(front->getRightChild());
            front->setRightChild(temp);
            if (front->getLeftChild()) {
                q.push(front->getLeftChild());
            }
            if (front->getRightChild()) {
                q.push(front->getRightChild());
            }
        }
    }

}

- lingguang1997 August 10, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void mirror()
	{
		trees temp = left;
		left = right;
		right = temp;
		if(left!=null)
		left.mirror();
		if(right!=null)
		right.mirror();
	}

- Vathul August 10, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

private void mirror(Node node) {
  if (node != null) {
    // do the sub-trees
    mirror(node.left);
    mirror(node.right);

    // swap the left/right pointers
    Node temp = node.left;
    node.left = node.right;
    node.right = temp;
  }
}

- Anonymous August 12, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

/*
 * create a mirrored tree
 */
tree_node_t* tree_create_mirror (tree_node_t* root)
{
    if (root == NULL) {
        return (NULL);
    }
    tree_node_t *new_root = (tree_node_t*)malloc(sizeof(tree_node_t));
    if (new_root == NULL) {
        printf("tree node malloc failure.\n");
        return (NULL);
    }
    new_root->value = root->value;
    new_root->left = tree_create_mirror(root->right);
    new_root->right = tree_create_mirror(root->left);
    return (new_root);
}

- ggmufc August 12, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void TreeOperation::mirrorTree(TreeNode* node){
	if(node != NULL){
		mirrorTree(node->getLeftChild());
		mirrorTree(node->rightChild);
		swapChildren(node);
	}
}


/**
* Swap the children of a given parent.
* Works only for binary tree.
*/
void TreeOperation::swapChildren(TreeNode* parent){
	TreeNode* temp;
	temp=parent->getLeftChild();
	parent->setLeftChild(parent->rightChild);
	parent->rightChild = temp;
}

- Rudra August 23, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

mirror(root){
	if(root!=null){
		if(root.left!=null){
			if(root.left.left!=null || root.left.right!=null)
				mirror(root.left);
		}
		else if(root.right!=null){
			if(root.right.left!=null || root.right.right!=null)
				mirror(root.right)
		}
		swap(root.left,root.right)
}

- Parin August 30, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

template<typename T>
struct node_traits
{
	typedef typename T type_value
	typedef typename const T type_value_const;
	typedef typename T& type_ref;
	typedef typename const T& type_ref_const;
	typedef typename T&& type_rref;
	typedef typename const T&& type_rref_const;
	typedef typename T* type_ptr;
	typedef typename const T* type_ptr_const; // pointer to const
	typedef typename const T* const type_const_ptr_const; // const pointer to const
	typedef typename T* const type_const_ptr; // constant pointer to non-const
	typedef std::shared_ptr<node> link;
};

template<typename T>
struct node
{
public :
	typedef node_traits<T>::type_value type_value;
	node(const T& x) : item(x) { l.reset(); r.reset(); }

private :
	T item;
	std::shared_ptr<node> l, r;
};

template<typename T>
class bst
{
public :
	typedef node_traits<T>::link link;
	friend void swap(link a, link b);
	bst() { head.reset(); }

	// ...
	link mirror()
	{
		return mirrorR(head);
	}

private :
	// ...
	link mirrorR(link h)
	{
		if(h == 0) return std::nullptr;

		mirrorR(h->l);
		mirrorR(h->r);

		swap(h->l, h->r);
	}

	link head;
}

void swap(link a, link b)
{
	link t(a);
	a = b;
	b = t;
}

- nilukush September 08, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

In

mirrorR

, missed :

return h;

in the end.

- nilukush September 08, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

package tree;


class TreeNode{
int data;
TreeNode leftNode;
TreeNode rightNode;

public TreeNode(int d){
this.data = d;
this.leftNode = null;
this.rightNode = null;
}
}

public class MirrorTree {

TreeNode root;
TreeNode currentNode;
int sum;

public MirrorTree(){
this.sum = 0;
this.root = null;
this.currentNode = null;
}

public boolean compareNodeValue(int currentNode, int newNode){
if(currentNode < newNode){
return true;
}
else{
return false;
}
}

public void add(int value){
TreeNode newNode = new TreeNode(value);
//newNode.data = value;
if(root == null){
root = newNode;
newNode = null;
currentNode = root;
}else{
currentNode = root;
if(compareNodeValue(currentNode.data, newNode.data)){
addRight(newNode);
}
else{
addLeft(newNode);
}}
}

public void addRight(TreeNode newRNode){
if(currentNode.rightNode == null){
currentNode.rightNode = newRNode;
newRNode = null;
}
else{
currentNode = currentNode.rightNode;
if(compareNodeValue(currentNode.data, newRNode.data)){
addRight(newRNode);
}
else{
addLeft(newRNode);
}
}
}

public void addLeft(TreeNode newLNode){
if(currentNode.leftNode == null){
currentNode.leftNode = newLNode;
newLNode = null;
}
else{
currentNode = currentNode.leftNode;
if(compareNodeValue(currentNode.data, newLNode.data)){
addRight(newLNode);
}
else{
addLeft(newLNode);
}
}
}

public void displayInorder(TreeNode rootNode) {

if (rootNode != null) {
displayInorder(rootNode.leftNode);
System.out.print(" " + rootNode.data);
displayInorder(rootNode.rightNode);
}
}

private void mirror(TreeNode node) {
if (node != null) {
mirror(node.leftNode);
mirror(node.rightNode);

TreeNode temp = node.leftNode;
node.leftNode = node.rightNode;
node.rightNode = temp;
}
}



public static void main(String[] args) {
MirrorTree obj = new MirrorTree();
obj.add(10);
obj.add(20);
obj.add(15);
obj.add(25);

obj.displayInorder(obj.root);
System.out.println(" ");
obj.mirror(obj.root);
System.out.println(" ");
System.out.println("Mirror Tree");
obj.displayInorder(obj.root);


}

}

- Astha sharma February 20, 2016 | 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