• 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

Rectangular chocolate bar Create at least one piece which consists of exactly nTiles tiles

Sort an array according to the order defined by another array

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

Reliance Jio Software Developer Interview Experience

Printing intermediate Integers between one element & next element of array

Binary Tree Isomorphic to each other

Number of Islands BFS/DFS

Edit Distance ( Dynamic Programming )

Subset Sum Problem Dynamic programming

Difference between a LinkedList and a Binary Search Tree BST

Get K Max and Delete K Max in stream of incoming integers

Serialise Deserialise N-ary Tree

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

VMWare SDEII Interview

CodeChef’ RRCOPY

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

25 horses 5 tracks Find 3 fastest puzzle

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

1014 Practice Question of New GRE – Princeton

Convert number to words java

Length of the longest substring without repeating characters

HackeEarth Flipkart’s Drone

Memory Efficient LinkedList

Longest Increasing Subsequence

Find two non repeating elements in an array of repeating elements

Facebook Interview Question : Interleave List

ADOBE Aptitude C Language Test

Implement a generic binary search algorithm for Integer Double String etc

SAP Off Campus Hiring_ March 2015 Sample Questions

C Program for TAIL command of UNIX

Copyright © 2023 · Genesis Framework · WordPress · Log in