Skip to main content

Posts

Showing posts with the label binary trees

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 hel...

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             ...

Top & Bottom View Of a Binary Tree Made Easy!

In the previous blog, I wrote about finding Left & Right view of a Binary Tree . In this blog, I'm going to talk about Top & Bottom view of a binary tree and will again try to create a single approach that will solve both the problems so instead of understanding two different approaches we'll discuss a single approach for it. Firstly, let us understand " What is the Top/Bottom view of a binary tree? ". When a tree is viewed from the Top/Bottom then the only nodes visible from that particular view are printed/included in output. For example : For the above mentioned binary tree nodes in Top & Bottom View are as follows : Top View -> 4, 2, 1, 3, 6 Bottom View -> 4, 7, 5, 8, 6 Let's try to understand it for the top view and the bottom view will be similar with minor changes. Looking from the top 4 is visible so we've included it in our top view, similarly, 2 is visible so we've included it in our top view, 7 is getting overlapped by 2 so we...

Left & Right View Of a Binary Tree Made Easy!

When talking about binary trees then the view of a binary tree is something that has the most probability to be asked in a coding interview. In this blog post, I'm going to talk mainly about the Right & Left views of a binary tree. There are so many ways available on the internet, that it's really a very difficult task to choose which method you should follow. Suppose, you're using one method for Left View and another method for Right View, now you need to know two different methods for two similar questions. In this blog we'll discuss a single approach by which you can solve both Left View and Right View problems.   Before diving directly into the approach we need to understand what exactly is this Right/Left view. For your better understanding I've created a binary tree : So let's try to understand the right view for this binary tree. Right View simply means looking at the tree from its right side and the only nodes visible from that view are required to b...

Tree Traversals Made Easy!

In this blog, I'm going to discuss one of the easiest ways by which you can traverse a binary tree. Structure Of Tree struct node { int data; node *right,*left; node(int data) { this->data=data; left=right=NULL; } }; Tree   traversal   Pre-order :  Root -> Left -> Right   void preorder(node* root) { if(root==NULL) // base condition return ; cout< data<<" "; // print root preorder(root->left); // go to left child preorder(root->right); // go to right child } In-order : Left -> Root -> Right void inorder(node* root) {     if(root==NULL)         return ;     inorder(root->left);     cout<<root->data<<" ";     inorder(root->right); } Post-order : Left -> Right -> Root  void postorder(node* root) {     if(root==NULL)         return ;   ...