Amazon Interview Question
Production EngineersTeam: kindle
Country: India
Interview Type: In-Person
T* createMirror(T* root)
{
if(root == NULL)
return;
T* tmp = new T;
tmp->val = root->val;
tmp->left = createMirror(root->right);
tmp->right = createMirror(root->left);
return tmp;
}
Node Swaptree(Struct Node* node)
{
while(node !== null)
{
Node temp1 = Swaptree(node->left);
Node temp2 = Swaptree(node->right);
node->left = temp2;
node->right = temp1;
}
return node;
}
void mirrorImage() {
java.util.Queue<Node> queue = new LinkedList<MirrorImage.Node>();
queue.add(root);
while (!queue.isEmpty()) {
Node node = queue.poll();
Node temp = node.right;
node.right = node.left;
node.left = temp;
if (node.left != null) {
queue.add(node.left);
}
if (node.right != null) {
queue.add(node.right);
}
}
}
<pre lang="" line="1" title="CodeMonkey53376" class="run-this">/* The class name doesn't have to be Main, as long as the class is not public. */
class Main
{
public static void main (String[] args) throws java.lang.Exception
{
java.io.BufferedReader r = new java.io.BufferedReader (new java.io.InputStreamReader (System.in));
String s;
while (!(s=r.readLine()).startsWith("42")) System.out.println(s);
}
}
</pre><pre title="CodeMonkey53376" input="yes">node* mirror(node *root){
mynode *temp;
if(root ==null)return null;
temp =(mynode *)malloc sizeof(node);
temp->value= root->value;
temp->lnode = mirror(root->rnode);
temp->rnode=mirror(root->lnode);
return(temp);
}
</pre>
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
struct node
{
int data;
struct node* left;
struct node* right;
};
typedef struct node Node;
Node* newNode (int data)
{
Node* temp =(Node*)malloc(sizeof(Node));
temp->data = data;
temp->left = NULL;
temp->right = NULL;
return temp;
}
Node* mirrorTree (Node* node)
{
Node* temp;
if(node->left==NULL && node->right==NULL)
return node;
else{
temp = node->left;
node->left = node->right;
node->right = temp;
mirrorTree(node->left);
mirrorTree(node->right);
return (node);
}
}
void printInorder (Node* node)
{
if(node == NULL)
return;
printInorder(node->left);
printf(" %d ",node->data);
printInorder(node->right);
}
int main ()
{
Node *root = newNode(1);
root->left = newNode(2);
root->right = newNode(3);
root->left->right = newNode(4);
root->left->left = newNode(5);
root->right->left = newNode(6);
root->right->right = newNode(7);
printf("Original tree is: \n");
printInorder(root);
root = mirrorTree(root);
printf("\nThe mirror tree is:\n");
printInorder(root);
getch();
return 0;
}
void mirror(TreeNode root)
- getjar.com/todotasklist my android app December 18, 2011{
if(root==null)
return;
TreeNode temp=root.left;
root.left=root.right;
root.right=temp;
mirror(root.left);
mirror(root.right);
}