• 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

Python List

Find the smallest window in a string containing all characters of another string

Find next greater number with same set of digits

FizzBuzz Solution C C++

Printing intermediate Integers between one element & next element of array

robot standing at first cell of an M*N matrix. It can move only in two directions, right and down. In how many ways, it can reach to the last cell i.e. (M, N) Code it

Stock Buy Sell to Maximize Profit

Amazon Interview On-Campus For Internship – 1

‘N’ Story Building, with 1,2,3 steps how many ways can a person reach top of building.

Puzzle : 100 doors in a row Visit and Toggle the door. What state the door will be after nth pass ?

Word Break Problem

Right view of Binary tree

Add Sub Multiply very large number stored as string

BlueStone E-commerce Interview Experience

In Given LinkedList Divide LL in N Sub parts and delete first K nodes of each part

SAP Interview Questions

Find min element in Sorted Rotated Array (Without Duplicates)

Find an index i such that Arr [i] = i in array of n distinct integers sorted in ascending order.

Introduction To Number Theory ( Part 1 )

Maximum difference between two elements s.t larger element appears after the smaller number

Find Percentage of Words matching in Two Strings

Hackerearth : Counting Subarrays

LeetCode: Binary Tree Maximum Path Sum

Calculate price of parking from parking start end time prices

VMWare SDEII Interview

Given a float number convert it into the string WITHOUT using any inbuilt Function

Find min element in Sorted Rotated Array (With Duplicates)

Python Array String

Find and print longest consecutive number sequence in a given sequence in O(n)

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

Copyright © 2025 · Genesis Framework · WordPress · Log in