• 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

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

Best Java Book | Top Java Programming Book for Beginners

SAP Hiring Off-Campus General Aptitude

CodeChef’ RRCOPY

Introduction To Number Theory ( Part 1 )

Find the element that appears once others appears thrice

Find position of the only set bit

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

Cisco Hiring Event 21st – 22nd Feb 2015

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

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

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

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

Knight Tour Problem (Graph – Breadth First Search)

Closed Parentheses checker

Printing Longest Common Subsequence

Given array of 0’s and 1’s. All 0’s are coming first followed by 1’s. find the position of first 1

LeetCode : Word Search

Difference between a LinkedList and a Binary Search Tree BST

Find shortest distances between every pair of vertices ( Dynamic Programming Floyd Warshall Algorithm)

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

Test Cases for Round Function

Python List

Implement LRU Cache

The greedy coins game Dynamic Programming

Level order traversal in Spiral form

Reverse a Linked List in groups of given size

Count Possible Decodings of a given Digit Sequence

Add Sub Multiply very large number stored as string

Right view of Binary tree

Copyright © 2026 · Genesis Framework · WordPress · Log in