• 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

Find loop in Singly linked list

Longest Increasing Subsequence

Printing Longest Common Subsequence

Naurki.com Security Breach

Print vertical sum of all the axis in the given binary tree

Find Pythagorean Triplets in an array in O(N)

Reverse a Linked List in groups of given size

Apriori algorithm C Code Data Mining

SAP Off Campus Hiring_ March 2015 Analytical Aptitude

Get K Max and Delete K Max in stream of incoming integers

SAP Off Campus Hiring_ March 2015 Computer Skills

Find the number ABCD such that when multipled by 4 gives DCBA.

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

Test Cases for Round Function

BlueStone E-commerce Interview Experience

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

Print Power Set of a Set

Linked List V/S Binary Search Tree

Trapping Rain Water

Flipkart Set 1 On Campus with Answers

Reliance Jio Software Developer Interview Experience

Check if an array has duplicate numbers in O(n) time and O(1) space

ADOBE Aptitude C Language Test

TicTacToe Game As Asked in Flipkart

Amazon Interview On-Campus For Internship – 1

Best Java Book | Top Java Programming Book for Beginners

Find min element in Sorted Rotated Array (Without Duplicates)

Hackerearth : Counting Subarrays

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

Check Binary Tree is Binary Search Tree or not

Copyright © 2026 · Genesis Framework · WordPress · Log in