• 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

Level order traversal in Spiral form

FizzBuzz Solution C C++

Top 10 Interviews Techniqes for Campus Interview in IIT NIT BITS for MTech

C++ OOPs Part2

Reversal of LinkedList

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

Trie Dictionary

C++ OOPs Part1

The Magic HackerEarth Nirvana solutions Hiring Challenge

Diagonal Traversal of Binary Tree

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

Find if a binary tree is height balanced ?

Check a String is SUBSEQUENCE of another String Find Minimum length for that ( DNA Matching )

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

Introduction To Number Theory ( Part 1 )

Handle duplicates in Binary Search Tree

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

Get Minimum element in O(1) from input numbers or Stack

Implement a generic binary search algorithm for Integer Double String etc

BFS (Breath First Search)

Serialise Deserialise N-ary Tree

Generic Object Oriented Stack with Template

Print all nodes that are at distance k from a leaf node

Convert number to words java

CodeChef’ RRCOPY

Find if two rectangles overlap

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

Python Dictionaries

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

N teams are participating. each team plays twice with all other teams. Some of them will go to the semi final. Find Minimum and Maximum number of matches that a team has to win to qualify for finals ?

Copyright © 2026 · Genesis Framework · WordPress · Log in