Skip to main content

How to Invert A Binary Tree (Iteratively + Recursively)?

Inverting a binary is one of the most common questions being asked these days. Often, seen many times in the ad of AlgoExpert as well.

So, what is an inverted binary tree?
Inverted binary tree is a simply a mirror image of given binary tree.
Example :


So, all we need to do is "Swapping left children with right children of a binary tree at every level."

Implementation

Recursively

 TreeNode* invertTree(TreeNode* root) 
{
if(root==NULL)
return NULL;
swap(root->left,root->right);
invertTree(root->left);
invertTree(root->right);

return root;
}
Iteratively
TreeNode* invertTree(TreeNode* root) 
{
if(root==NULL)
return NULL;
queue<TreeNode*> q;
q.push(root);
while(!q.empty())
{
TreeNode* c= q.front();
q.pop();
swap(c->left,c->right);
if(c->left!=NULL)
q.push(c->left);
if(c->right!=NULL)
q.push(c->right);
}
return root;
}

I hope this tutorial helped you!
Happy Coding!

Comments

Most Popular Posts

100+ STL Algorithms, "I Bet If You Know Even 20 Out of These..." ( C++ 17 )

So, In the previous posts, I've been telling you about the various containers in STL and their implementation. I left a few of them purposely such as unordered_map, unordred_set, etc. so that you also do some homework. This post will entirely be about various functions available in STL and it would be your task to completely code & implement them. Do tell me if you have any doubts related to it in the comment section below. Some common Data Structures Implementation In STL : Stack Syntax : stack<data_type> stack_name Example  : stack<int> st; Built-in functions such as top() ,  push() , pop() , empty() , size() etc. are available in STL for Stack. Queue Syntax :  queue<data_type> queue_name Example  :  queue<int> q; Built-in functions such as push() , pop() , empty() , size() , front() , back() are available in STL for Queue. List ( Doubly Linked List ) Syntax :  list<data_type> list_name Exampl...

Zig-Zag & Spiral Traversal Of A Binary Tree Made Easy

Zig-Zag and Spiral Traversal of a Binary Tree are some of those problems which appear different but have a lot of things in common and thus, can also be solved easily using a common approach. Let's understand what a Zig-Zag & Spiral traversal exactly is : Zig-Zag traversal for the above binary tree is :                                                                                1                                                                            3      2             ...

The Best Programming Language To Learn

In this world of technology, everyone wants to have a secure career. All the aspirants are driven  through  trending languages that are coming in the market such as Python, Ruby, Scala, etc. I'm not writing this blog to stop anyone from programming in Python etc. but I'm going to share with you all the  ultimate  truth of the industry. If you're a Computer Science student and will be appearing for an interview at placements then you' ll be judged  on your various skills. Modern languages such as Python, Scala, etc. all have inbuilt functions and a lot of data abstraction is present there. When you're in an interview then you' ll be judged  upon your  proficiency  in a language and how clear your logics are that is also checked there. So, there you  are asked  to  write down  a code and this is important as this will show the interviewer how capable you are. For example : A question on sorting is asked so in Python you ca...