• 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 two non repeating elements in an array of repeating elements

C Program for TAIL command of UNIX

Sequence Finder Dynamic Programming

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

Walmart Labs Interview Experience

Daughter’s Age VeryGood Puzzle

Circular Linked List

Subset Sum Problem Dynamic programming

TicTacToe Game As Asked in Flipkart

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

Generate largest number arranging a no. of given non negative integer numbers

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

LeetCode: Binary Tree Maximum Path Sum

Right view of Binary tree

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

K’th Largest Element in BST when modification to BST is not allowed

Password Predictor

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 ?

Printing Longest Common Subsequence

Find loop in Singly linked list

Python Array String

Find position of the only set bit

Print Power Set of a Set

building with N steps, we can take 1,2,3 steps calculate number of ways to reach at top of building

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

Microsoft BING Interview Experience

Skiing on Mountains Matrix

Naurki.com Security Breach

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

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

Copyright © 2026 · Genesis Framework · WordPress · Log in