• Skip to primary navigation
  • Skip to content
  • Skip to primary sidebar
  • Skip to secondary sidebar

GoHired

Interview Questions asked in Google, Microsoft, Amazon

Join WeekEnd Online Batch from 4-April-2020 on How to Crack Coding Interview in Just 10 Weeks : Fees just 20,000 INR

  • Home
  • Best Java Books
  • Algorithm
  • Internship
  • Certificates
  • About Us
  • Contact Us
  • Privacy Policy
  • Array
  • Stack
  • Queue
  • LinkedList
  • DP
  • Strings
  • Tree
  • Mathametical
  • Puzzles
  • Graph

Mirror of Tree

February 10, 2015 by Dhaval Dave

Write a code to construct a Mirror of Binary Tree as below.

Solution 1 ) Create a New Tree and store nodes in the way we want (mirrored way)

Algo :

struct node{
    int value;
    struct node* left;
    struct node* right;
};
struct node *mirrorTree(struct node *root){
  struct node *temp;
  if(root==NULL)
    return(NULL);
  temp = (struct node *) malloc(sizeof(struct node));
  temp->value = root->value;
  temp->left  = mirrorTree(root->right);
  temp->right = mirrorTree(root->left);
  return(temp);
}
Solution 2 ) In place change ( Use Recursion )

 

void mirror(struct node* node) 
{
  if (node==NULL) 
    return;  
  else
  {
    struct node* temp;

    mirror(node->left);
    mirror(node->right);
 
    /* swap the pointers */
      temp        = node->left;
      node->left  = node->right;
      node->right = temp;
  }
}
See working Code at : http://ideone.com/uDrfeu
See Video : 

Similar Articles

Filed Under: Amazon Interview Question, Interview Questions, problem Tagged With: Binary Tree, tree

Reader Interactions

Primary Sidebar

Join WeekEnd Online/Offline Batch from 4-April-2020 on How to Crack Coding Interview in Just 10 Weeks : Fees just 20,000 INR

Join WeekEnd Online/Offline Batch from 4-April-2020

WhatsApp us

Secondary Sidebar

Custom Search

  • How I cracked AMAZON
  • LeetCode
  • Adobe
  • Amazon
  • Facebook
  • Microsoft
  • Hacker Earth
  • CSE Interview

Top Rated Questions

How Radix sort works

Difference between a LinkedList and a Binary Search Tree BST

strtok()

N Petrol bunks or City arranged in circle. You have Fuel and distance between petrol bunks. Is it possible to find starting point so that we can travel all Petrol Bunks

Search element in a matrix with all rows and columns in sorted order

Wrong Directions given find minimum moves so that he can reach to the destination

Printing each word reverse in string

Linked List V/S Binary Search Tree

BFS (Breath First Search)

flattens 2 D linked list to a single sorted link list

Reversal of LinkedList

Rectangular chocolate bar Create at least one piece which consists of exactly nTiles tiles

Urban Ladder Written Test.

Binary Tree Isomorphic to each other

Find position of the only set bit

Introduction To Number Theory ( Part 1 )

Possible sizes of bus to carry n groups of friends

There are N nuts and N bolts, u have to find all the pairs of nuts and bolts in minimum no. of iteration

Word Break Problem

write a c program that given a set a of n numbers and another number x determines whether or not there exist two elements in s whose sum is exactly x

SAP Interview Questions

Amazon Interview Experience – SDE Chennai

Adobe Interview Questions 8 month Exp

Find Percentage of Words matching in Two Strings

Add Sub Multiply very large number stored as string

Facebook Interview Question : Interleave List

Leetcode: Edit Distance

TicTacToe Game As Asked in Flipkart

simple sql injection

Cisco Hiring Event 21st – 22nd Feb 2015

Copyright © 2026 · Genesis Framework · WordPress · Log in