• 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 Pythagorean Triplets in an array in O(N)

HackeEarth Flipkart’s Drone

Find the kth number with prime factors 3, 5 and 7

LeetCode: Container With Most Water

FizzBuzz Solution C C++

ADOBE Aptitude C Language Test

Find Nearest Minimum number in left side in O(n)

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

TicTacToe Game As Asked in Flipkart

Closed Parentheses checker

Find two non repeating elements in an array of repeating elements

Urban Ladder Written Test.

Maximum size of square sub matrix with all 1’s in a binary matrix

SAP Off Campus Hiring_ March 2015 Computer Skills

Circular Linked List

Leetcode: Edit Distance

Handle duplicates in Binary Search Tree

CodeChef Code SGARDEN

Level order traversal in Spiral form

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

Given a string, find the first character which is non-repetitive

Naurki.com Security Breach

Generic Object Oriented Stack with Template

Find Percentage of Words matching in Two Strings

Find if a binary tree is height balanced ?

Subset Sum Problem Dynamic programming

Given Set of words or A String find whether chain is possible from these words or not

Printing intermediate Integers between one element & next element of array

Python Dictionaries

Binary Tree in Java

Copyright © 2026 · Genesis Framework · WordPress · Log in