• 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

Facebook Interview Question : Interleave List

LeetCode: Container With Most Water

Apriori algorithm C Code Data Mining

DFS (Depth First Search)

Trie Dictionary

Introduction To Number Theory ( Part 1 )

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

Find if two rectangles overlap

Find next greater number with same set of digits

N Petrol bunks or City arranged in circle. You have Fuel and distance between petrol bunks. Is it possible to find starting point so that we can travel all Petrol Bunks

Print Power Set of a Set

Given a sorted array and a number x, find the pair in array whose sum is closest to x

SAP Off Campus Hiring_ March 2015 Verbal Skills

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

Max Sum in circularly situated Values

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

BlueStone E-commerce Interview Experience

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

Test Cases for Round Function

Binary Tree in Java

Find min element in Sorted Rotated Array (With Duplicates)

Implement a generic binary search algorithm for Integer Double String etc

Memory Efficient LinkedList

Maximum path sum between two leaves

Connect n ropes with minimum cost

System Design: Designing a LLD for Hotel Booking

Common Ancestor in a Binary Tree or Binary Search Tree

Add Sub Multiply very large number stored as string

Reliance Jio Software Developer Interview Experience

Maximum sum contiguous subarray of an Array

Copyright © 2026 · Genesis Framework · WordPress · Log in