• 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

Check Binary Tree is Binary Search Tree or not

BFS (Breath First Search)

Advanced SQL Injection

Apriori algorithm C Code Data Mining

Find two non repeating elements in an array of repeating elements

Handle duplicates in Binary Search Tree

Sort Stack in place

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

Subset Sum Problem Dynamic programming

Singly linked list

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

Print Power Set of a Set

System Design: Designing a LLD for Hotel Booking

LeetCode: Binary Tree Maximum Path Sum

Convert number to words java

Find the element that appears once others appears thrice

Linked List V/S Binary Search Tree

How Radix sort works

Skiing on Mountains Matrix

Regular Expression Matching

Edit Distance ( Dynamic Programming )

Check if an array has duplicate numbers in O(n) time and O(1) space

Client Server C program

Cisco Hiring Event 21st – 22nd Feb 2015

SAP Off Campus Hiring_ March 2015 Analytical Aptitude

flattens 2 D linked list to a single sorted link list

Generic Object Oriented Stack with Template

Spanning Tree

Urban Ladder Written Test.

Facebook Interview Question : Interleave List

Copyright © 2026 · Genesis Framework · WordPress · Log in