<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Helpmestudybro</title>
	<atom:link href="https://www.helpmestudybro.com/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.helpmestudybro.com/</link>
	<description>Learn Computer Science </description>
	<lastBuildDate>Tue, 09 May 2023 10:10:53 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.5.8</generator>

<image>
	<url>https://www.helpmestudybro.com/wp-content/uploads/2020/07/cropped-HSB-Logo-32x32.png</url>
	<title>Helpmestudybro</title>
	<link>https://www.helpmestudybro.com/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Print All Nodes of Binary Tree That Do not Have Siblings</title>
		<link>https://www.helpmestudybro.com/print-all-nodes-of-binary-tree-that-do-not-have-siblings/</link>
		
		<dc:creator><![CDATA[admin]]></dc:creator>
		<pubDate>Tue, 26 Jan 2021 19:50:17 +0000</pubDate>
				<category><![CDATA[Binary Tree]]></category>
		<category><![CDATA[data structure]]></category>
		<guid isPermaLink="false">https://www.helpmestudybro.com/?p=1289</guid>

					<description><![CDATA[<p>“Print All Nodes of Binary Tree That Do not Have Siblings” is one of foremost algorithmic problem asked in a technical interview of product-based companies. Here, we are given a binary tree and our task is to write a program to print all nodes of binary tree that don’t have&#46;&#46;&#46;</p>
<p>The post <a href="https://www.helpmestudybro.com/print-all-nodes-of-binary-tree-that-do-not-have-siblings/">Print All Nodes of Binary Tree That Do not Have Siblings</a> appeared first on <a href="https://www.helpmestudybro.com">Helpmestudybro</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>“<strong>Print All Nodes of Binary Tree That Do not Have Siblings</strong>” is one of foremost algorithmic problem asked in a technical interview of product-based companies. Here, we are given a binary tree and our task is to write a program to print all nodes of binary tree that don’t have siblings.</p>



<p><strong>Sibling Nodes</strong>: Two Nodes are said to be sibling nodes if the parent node of both nodes is same. Any Node can have at most one sibling. Root node does not have any sibling.</p>


<div class="wp-block-image">
<figure class="aligncenter size-large"><img fetchpriority="high" decoding="async" width="562" height="321" src="https://www.helpmestudybro.com/wp-content/uploads/2021/01/NodesWithoutSiblings.png" alt="Print All Nodes of Binary Tree That Don’t Have Siblings" class="wp-image-1290" srcset="https://www.helpmestudybro.com/wp-content/uploads/2021/01/NodesWithoutSiblings.png 562w, https://www.helpmestudybro.com/wp-content/uploads/2021/01/NodesWithoutSiblings-300x171.png 300w" sizes="(max-width: 562px) 100vw, 562px" /></figure></div>


<p>We can solve the following problem both recursively and iteratively. Here, the idea is that for any node (not leaf node), check if it has only one child node or have both child nodes. If only one child node is present, then print that node and continue traversal. If both child nodes are present, then simply continue traversal.</p>



<p><strong>METHOD 1: Recursive Solution</strong></p>



<h4 class="wp-block-heading">C++ Program to print all nodes of Binary Tree that do not have siblings is as follows:</h4>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: cpp; title: ; notranslate">
/* C++ Program to print all nodes of Binary Tree that do not have siblings */
#include&lt;bits/stdc++.h&gt;
using namespace std;

typedef struct Node
{
    int data;
    struct Node *left;
    struct Node *right;
    Node(int ele)
    {
        data = ele;
        left = NULL;
        right = NULL;
    }
} Node;

/* Function to print all nodes of Binary Tree that don’t have siblings */
void printSingleSiblings(Node *root)
{
    /* Bssic Case: Null Tree */
    if(root == NULL)
    return;
    
    /* If only right child is present, print it and recursively traversel right subtree */
    if(root -&gt; left == NULL &amp;&amp; root -&gt; right != NULL)
    {
        cout&lt;&lt;root -&gt; right -&gt; data&lt;&lt;&quot; &quot;;
        printSingleSiblings(root -&gt; right);
    }
    
    /* If only left child is present, print it and recursively traversel left subtree */
    else if(root -&gt; left != NULL &amp;&amp; root -&gt; right == NULL)
    {
        cout&lt;&lt;root -&gt; left -&gt; data&lt;&lt;&quot; &quot;;
        printSingleSiblings(root -&gt; left);
    }
    
    /* If both childs are present, recursively traverse them */
    else
    {
        printSingleSiblings(root -&gt; left);
        printSingleSiblings(root -&gt; right);
    }
}

int main()
{
    /* Creating a Binary trees and inserting some nodes in it */
    Node *root = NULL;
  
    
    root = new Node(1);
    root -&gt; left = new Node(2);
    root -&gt; right = new Node(3);
    root -&gt; left -&gt; left = new Node(4);
    root -&gt; left -&gt; right = new Node(5);
    root -&gt; right -&gt; left = new Node(6);
    root -&gt; right -&gt; right = new Node (7);
    root -&gt; left -&gt; right -&gt; left = new Node(8);
    root -&gt; right -&gt; left -&gt; right = new Node(9);
    
    /* Calling function to print all nodes of Binary Tree that do not have siblings */
    cout&lt;&lt;&quot;Nodes in a tree that don&#039;t have siblings are:\n&quot;;
    printSingleSiblings(root);
}
</pre></div>


<p><strong>OUTPUT:</strong></p>



<pre class="wp-block-preformatted">Nodes in a tree that don't have siblings are:
8 9</pre>



<p><strong>METHOD 2: Iterative Solution</strong></p>



<h4 class="wp-block-heading">C++ Program to print all nodes of Binary Tree that do not have siblings is as follows:</h4>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: cpp; title: ; notranslate">
/* C++ Program to print all nodes of Binary Tree that do not have siblings */
#include&lt;bits/stdc++.h&gt;
using namespace std;

typedef struct Node
{
    int data;
    struct Node *left;
    struct Node *right;
    Node(int ele)
    {
        data = ele;
        left = NULL;
        right = NULL;
    }
} Node;

/* Function to print all nodes of Binary Tree that do not have siblings */
void printSingleSiblings(Node *root)
{
    /* Bssic Case: Null Tree */
    if(root == NULL)
    return;
    
   queue&lt;Node *&gt; que;
   que.push(root);
   
   while(!que.empty())
   {
       Node *temp = que.front();
       
       que.pop();
       
       if(temp -&gt; left == NULL &amp;&amp; temp -&gt; right != NULL)
       {
           cout&lt;&lt;temp -&gt; right -&gt; data&lt;&lt;&quot; &quot;;
           que.push(temp -&gt; right);
       }
       else if(temp -&gt; left != NULL &amp;&amp; temp -&gt; right == NULL)
       {
           cout&lt;&lt;temp -&gt; left -&gt; data&lt;&lt;&quot; &quot;;
           que.push(temp -&gt; left);
       }
       else if(temp -&gt; left != NULL &amp;&amp; temp -&gt; right != NULL)
       {
           que.push(temp -&gt; left);
           que.push(temp -&gt; right);
       }
   }
}

int main()
{
    /* Creating a Binary trees and inserting some nodes in it */
    Node *root = NULL;
  
    
    root = new Node(1);
    root -&gt; left = new Node(2);
    root -&gt; right = new Node(3);
    root -&gt; left -&gt; left = new Node(4);
    root -&gt; left -&gt; right = new Node(5);
    root -&gt; right -&gt; left = new Node(6);
    root -&gt; right -&gt; right = new Node (7);
    root -&gt; left -&gt; right -&gt; left = new Node(8);
    root -&gt; right -&gt; left -&gt; right = new Node(9);
    
    /* Calling function to print all nodes of Binary Tree that do not have siblings */
    cout&lt;&lt;&quot;Nodes in a tree that don&#039;t have siblings are:\n&quot;;
    printSingleSiblings(root);
}

</pre></div>


<p><strong>OUTPUT:</strong></p>



<pre class="wp-block-preformatted">Nodes in a tree that don't have siblings are:
8 9</pre>



<hr class="wp-block-separator has-css-opacity is-style-wide"/>



<p><strong>Related Posts:</strong></p>



<ol type="1"><li><a href="https://www.helpmestudybro.com/print-all-leaf-nodes-of-a-binary-tree/"><strong>Print All Leaf Nodes of a Binary Tree</strong></a></li><li><a href="https://www.helpmestudybro.com/count-number-of-nodes-in-a-binary-tree/"><strong>Count Number of Nodes in a Binary Tree</strong></a></li><li><a href="https://www.helpmestudybro.com/print-alternate-levels-of-a-binary-tree/"><strong>Print Alternate Levels of Binary Tree</strong></a></li><li><a href="https://www.helpmestudybro.com/maximum-width-of-binary-tree/"><strong>Maximum Width of Binary Tree</strong></a></li><li><a href="https://www.helpmestudybro.com/level-order-tree-traversal/"><strong>Level Order Tree Traversal</strong></a></li><li><a href="https://www.helpmestudybro.com/left-view-of-binary-tree/"><strong>Left View of Binary Tree</strong></a></li><li><a href="https://www.helpmestudybro.com/right-view-of-binary-tree/"><strong>Right View of Binary Tree</strong></a></li><li><a href="https://www.helpmestudybro.com/compute-height-of-a-binary-tree/"><strong>Compute Height of Binary Tree</strong></a></li><li><a href="https://www.helpmestudybro.com/inorder-tree-traversal-using-stack/"><strong>Inorder Tree Traversal Using Stack</strong></a></li><li><a href="https://www.helpmestudybro.com/preorder-traversal-of-binary-tree-using-stack/"><strong>Preorder Tree Trasversal Using Stack</strong></a></li><li><a href="https://www.helpmestudybro.com/postorder-traversal-of-binary-tree-using-stacks/"><strong>Postorder Tree Traversal Using Stack</strong></a></li><li><a href="https://www.helpmestudybro.com/vertical-traversal-of-binary-tree/"><strong>Vertical Order Tree Traversal</strong></a></li><li><a href="https://www.helpmestudybro.com/top-view-of-binary-tree/"><strong>Top View of Binary Tree</strong></a></li><li><a href="https://www.helpmestudybro.com/bottom-view-of-binary-tree/"><strong>Bottom View of Binary Tree</strong></a></li><li><a href="https://www.helpmestudybro.com/delete-binary-tree/"><strong>Delete Complete Binary Tree</strong></a></li><li><a href="https://www.helpmestudybro.com/check-if-two-trees-are-mirror-tree-of-each-other/"><strong>Check if two trees are mirror Trees of Each Other</strong></a></li><li><a href="https://www.helpmestudybro.com/convert-binary-tree-to-its-mirror-tree/"><strong>Convert Binary Tree to its Mirror Tree</strong></a></li><li><a href="https://www.helpmestudybro.com/check-if-binary-tree-is-symmetric-or-not/"><strong>Check if Binary Tree is Symmetric or Not</strong></a></li><li><a href="https://www.helpmestudybro.com/print-all-root-to-leaf-paths-in-a-binary-tree/"><strong>Print All Root to Leaf Paths in a Binary Tree</strong></a></li><li><a href="https://www.helpmestudybro.com/determine-if-two-binary-trees-are-identical-or-not/"><strong>Determine if two binary trees are identical or not</strong></a></li></ol>



<hr class="wp-block-separator has-css-opacity is-style-wide"/>
<p>The post <a href="https://www.helpmestudybro.com/print-all-nodes-of-binary-tree-that-do-not-have-siblings/">Print All Nodes of Binary Tree That Do not Have Siblings</a> appeared first on <a href="https://www.helpmestudybro.com">Helpmestudybro</a>.</p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>Determine if two binary trees are identical or not</title>
		<link>https://www.helpmestudybro.com/determine-if-two-binary-trees-are-identical-or-not/</link>
		
		<dc:creator><![CDATA[admin]]></dc:creator>
		<pubDate>Tue, 26 Jan 2021 19:32:26 +0000</pubDate>
				<category><![CDATA[Binary Tree]]></category>
		<category><![CDATA[data structure]]></category>
		<guid isPermaLink="false">https://www.helpmestudybro.com/?p=1284</guid>

					<description><![CDATA[<p>“Determine if two binary trees are identical or not”&#160;is a basic problem of tree data structure. Here, we are given two binary trees and our task is to write a program to determine if given two binary trees are identical or not. We can solve this problem both recursively and&#46;&#46;&#46;</p>
<p>The post <a href="https://www.helpmestudybro.com/determine-if-two-binary-trees-are-identical-or-not/">Determine if two binary trees are identical or not</a> appeared first on <a href="https://www.helpmestudybro.com">Helpmestudybro</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p><strong>“Determine if two binary trees are identical or not”&nbsp;</strong>is a basic problem of tree data structure. Here, we are given two binary trees and our task is to write a program to determine if given two binary trees are identical or not.</p>


<div class="wp-block-image">
<figure class="aligncenter size-large"><img decoding="async" width="602" height="321" src="https://www.helpmestudybro.com/wp-content/uploads/2021/01/IdenticalTrees.png" alt="Determine if two binary trees are identical or not" class="wp-image-1285" srcset="https://www.helpmestudybro.com/wp-content/uploads/2021/01/IdenticalTrees.png 602w, https://www.helpmestudybro.com/wp-content/uploads/2021/01/IdenticalTrees-300x160.png 300w" sizes="(max-width: 602px) 100vw, 602px" /></figure></div>


<p>We can solve this problem both recursively and iteratively. The basic idea in both the solution is to check whether root node of first tree is equal to root node of second tree. If value of root node of both trees is equal then, we recursively or iteratively traverse the left subtree and right subtree and check whether left subtree of first tree is equal to left subtree of second tree and right subtree of first tree is equal to right subtree of second tree.</p>



<p>If at any point of traversal and comparison, the left and right subtree of first tree is not equal to left and right subtree of second tree, then we return false.</p>



<p><strong>Method 1: Recursive Solution</strong></p>



<h4 class="wp-block-heading">C++ Program to Determine if two binary trees are identical or not is as follows:</h4>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: cpp; title: ; notranslate">
/* C++ Program to determine if two binary trees are identical or not */
#include&lt;bits/stdc++.h&gt;
using namespace std;

typedef struct Node
{
    int data;
    struct Node *left;
    struct Node *right;
    Node(int ele)
    {
        data = ele;
        left = NULL;
        right = NULL;
    }
} Node;

/* Function to determine if two binary trees are identical or not */
bool checkidentical(Node *root1, Node *root2)
{
    /* Basic Case: Both Trees are empty */
    if(root1 == NULL &amp;&amp; root2 == NULL)
    {
        return true;
    }
    
    /* If one tree is empty and other not */
    if(root1 == NULL || root2 == NULL)
    {
        return false;
    }
    
    /*
        If data of nodes are equal, then recursively check left
        subtree and right subtree of both given binary trees
    */
    if(root1 -&gt; data == root2 -&gt; data)
    {
        return checkidentical(root1 -&gt; left, root2 -&gt; left) &amp;&amp;
        checkidentical(root1 -&gt; right, root2 -&gt; right);
    }
    else
    {
        return false;
    }
}

int main()
{
    /* Creating a Binary trees and inserting some nodes in it */
    Node *root1 = NULL;
    Node *root2 = NULL;
    
    root1 = new Node(1);
    root1 -&gt; left = new Node(2);
    root1 -&gt; right = new Node(3);
    root1 -&gt; left -&gt; left = new Node(4);
    root1 -&gt; left -&gt; right = new Node(5);
    root1 -&gt; right -&gt; left = new Node(6);
    root1 -&gt; right -&gt; right = new Node (7);
    root1 -&gt; left -&gt; right -&gt; left = new Node(8);
    root1 -&gt; right -&gt; left -&gt; right = new Node(9);
    
    root2 = new Node(1);
    root2 -&gt; left = new Node(2);
    root2 -&gt; right = new Node(3);
    root2 -&gt; left -&gt; left = new Node(4);
    root2 -&gt; left -&gt; right = new Node(5);
    root2 -&gt; right -&gt; left = new Node(6);
    root2 -&gt; right -&gt; right = new Node (7);
    root2 -&gt; left -&gt; right -&gt; left = new Node(8);
    root2 -&gt; right -&gt; left -&gt; right = new Node(9);
    
    
    /* Calling function to determine if two binary trees are identical or not */
    if(checkidentical(root1, root2) == true)
    {
        cout&lt;&lt;&quot;Given Binary Trees are Identical!!&quot;;
    }
    else
    {
        cout&lt;&lt;&quot;Given Binary Tree are not Identical!!&quot;;
    }
}
</pre></div>


<p><strong>OUTPUT:</strong></p>



<pre class="wp-block-preformatted">Given Binary Trees are Identical!!</pre>



<p><strong>Method 2: Iterative Solution</strong></p>



<h4 class="wp-block-heading">C++ Program to Determine if two binary trees are identical or not is as follows:</h4>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: cpp; title: ; notranslate">
/* C++ Program to determine if two binary trees are identical or not */
#include&lt;bits/stdc++.h&gt;
using namespace std;

typedef struct Node
{
    int data;
    struct Node *left;
    struct Node *right;
    Node(int ele)
    {
        data = ele;
        left = NULL;
        right = NULL;
    }
} Node;

/* Function to determine if two binary trees are identical or not */
bool checkidentical(Node *root1, Node *root2)
{
     /* Basic Case: Both Trees are empty */
    if(root1 == NULL &amp;&amp; root2 == NULL)
    {
        return true;
    }
    
    /* If one tree is empty and other not */
    if(root1 == NULL || root2 == NULL)
    {
        return false;
    }
    
    /* Initialize queue and push root into it */
    queue&lt;Node *&gt; que1;
    queue&lt;Node *&gt; que2;
    
    que1.push(root1);
    que2.push(root2);
    
    while(!que1.empty() &amp;&amp; !que2.empty())
    {
        Node *temp1 = que1.front();
        Node *temp2 = que2.front();
        
        /* 
            If data of both nodes are same, 
            then traverse trees further
        */
        if(temp1 -&gt; data == temp2 -&gt; data)
        {
            que1.pop();
            que2.pop();
            
            /*
                If left child of both tree exists, push it into queue
            */
            if(temp1 -&gt; left &amp;&amp; temp2 -&gt; left)
            {
                que1.push(temp1 -&gt; left);
                que2.push(temp2 -&gt; left);
            }
            /*
                If left child of only one tree exists, then return false.
            */
            else if(temp1 -&gt; left || temp2 -&gt; left)
            {
                return false;
            }
            
            /*
                If right child of both tree exists, push it into queue
            */
            if(temp1 -&gt; right &amp;&amp; temp2 -&gt; right)
            {
                que1.push(temp1 -&gt; right);
                que2.push(temp2 -&gt; right);
            }
            /*
                If right child of only one tree exists, then return false.
            */
            else if(temp1 -&gt; right || temp2 -&gt; right)
            {
                return false;
            }
        }
        /*
            If data of both nodes are not same, 
            then return false
        */
        else
        {
            return false;
        }
    }
    return true;
}

int main()
{
    /* Creating a Binary trees and inserting some nodes in it */
    Node *root1 = NULL;
    Node *root2 = NULL;
    
    root1 = new Node(1);
    root1 -&gt; left = new Node(2);
    root1 -&gt; right = new Node(3);
    root1 -&gt; left -&gt; left = new Node(4);
    root1 -&gt; left -&gt; right = new Node(5);
    root1 -&gt; right -&gt; left = new Node(6);
    root1 -&gt; right -&gt; right = new Node (7);
    root1 -&gt; left -&gt; right -&gt; left = new Node(8);
    root1 -&gt; right -&gt; left -&gt; right = new Node(9);
    
    root2 = new Node(1);
    root2 -&gt; left = new Node(2);
    root2 -&gt; right = new Node(3);
    root2 -&gt; left -&gt; left = new Node(4);
    root2 -&gt; left -&gt; right = new Node(5);
    root2 -&gt; right -&gt; left = new Node(6);
    root2 -&gt; right -&gt; right = new Node (7);
    root2 -&gt; left -&gt; right -&gt; left = new Node(8);
    root2 -&gt; right -&gt; left -&gt; right = new Node(9);
    
    
    /* Calling function to determine if two binary trees are identical or not */
    if(checkidentical(root1, root2) == true)
    {
        cout&lt;&lt;&quot;Given Binary Trees are Identical!!&quot;;
    }
    else
    {
        cout&lt;&lt;&quot;Given Binary Tree are not Identical!!&quot;;
    }
}

</pre></div>


<p><strong>OUTPUT:</strong></p>



<pre class="wp-block-preformatted">Given Binary Trees are Identical!!</pre>



<hr class="wp-block-separator has-css-opacity is-style-wide"/>



<p><strong>Related Posts:</strong></p>



<ol type="1"><li><a href="https://www.helpmestudybro.com/binary-tree-traversal/"><strong>Binary Tree Traversals</strong></a></li><li><a href="https://www.helpmestudybro.com/print-all-leaf-nodes-of-a-binary-tree/"><strong>Print All Leaf Nodes of a Binary Tree</strong></a></li><li><a href="https://www.helpmestudybro.com/count-number-of-nodes-in-a-binary-tree/"><strong>Count Number of Nodes in a Binary Tree</strong></a></li><li><a href="https://www.helpmestudybro.com/print-alternate-levels-of-a-binary-tree/"><strong>Print Alternate Levels of Binary Tree</strong></a></li><li><a href="https://www.helpmestudybro.com/maximum-width-of-binary-tree/"><strong>Maximum Width of Binary Tree</strong></a></li><li><a href="https://www.helpmestudybro.com/level-order-tree-traversal/"><strong>Level Order Tree Traversal</strong></a></li><li><a href="https://www.helpmestudybro.com/left-view-of-binary-tree/"><strong>Left View of Binary Tree</strong></a></li><li><a href="https://www.helpmestudybro.com/right-view-of-binary-tree/"><strong>Right View of Binary Tree</strong></a></li><li><a href="https://www.helpmestudybro.com/compute-height-of-a-binary-tree/"><strong>Compute Height of Binary Tree</strong></a></li><li><a href="https://www.helpmestudybro.com/inorder-tree-traversal-using-stack/"><strong>Inorder Tree Traversal Using Stack</strong></a></li><li><a href="https://www.helpmestudybro.com/preorder-traversal-of-binary-tree-using-stack/"><strong>Preorder Tree Trasversal Using Stack</strong></a></li><li><a href="https://www.helpmestudybro.com/postorder-traversal-of-binary-tree-using-stacks/"><strong>Postorder Tree Traversal Using Stack</strong></a></li><li><a href="https://www.helpmestudybro.com/vertical-traversal-of-binary-tree/"><strong>Vertical Order Tree Traversal</strong></a></li><li><a href="https://www.helpmestudybro.com/top-view-of-binary-tree/"><strong>Top View of Binary Tree</strong></a></li><li><a href="https://www.helpmestudybro.com/bottom-view-of-binary-tree/"><strong>Bottom View of Binary Tree</strong></a></li><li><a href="https://www.helpmestudybro.com/delete-binary-tree/"><strong>Delete Complete Binary Tree</strong></a></li><li><a href="https://www.helpmestudybro.com/check-if-two-trees-are-mirror-tree-of-each-other/"><strong>Check if two trees are mirror Trees of Each Other</strong></a></li><li><a href="https://www.helpmestudybro.com/convert-binary-tree-to-its-mirror-tree/"><strong>Convert Binary Tree to its Mirror Tree</strong></a></li><li><a href="https://www.helpmestudybro.com/check-if-binary-tree-is-symmetric-or-not/"><strong>Check if Binary Tree is Symmetric or Not</strong></a></li><li><a href="https://www.helpmestudybro.com/print-all-root-to-leaf-paths-in-a-binary-tree/"><strong>Print All Root to Leaf Paths in a Binary Tree</strong></a></li></ol>



<hr class="wp-block-separator has-css-opacity is-style-wide"/>
<p>The post <a href="https://www.helpmestudybro.com/determine-if-two-binary-trees-are-identical-or-not/">Determine if two binary trees are identical or not</a> appeared first on <a href="https://www.helpmestudybro.com">Helpmestudybro</a>.</p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>Print all Root to Leaf Paths in a Binary Tree</title>
		<link>https://www.helpmestudybro.com/print-all-root-to-leaf-paths-in-a-binary-tree/</link>
		
		<dc:creator><![CDATA[admin]]></dc:creator>
		<pubDate>Sun, 24 Jan 2021 19:04:23 +0000</pubDate>
				<category><![CDATA[Binary Tree]]></category>
		<category><![CDATA[data structure]]></category>
		<guid isPermaLink="false">https://www.helpmestudybro.com/?p=1248</guid>

					<description><![CDATA[<p>“Print all Root to Leaf Paths in a Binary Tree” is an important algorithmic problem based on tree data. Here, we are given a binary tree and our task is to write a program to print all root to leaf paths in given binary tree. Here, the idea is to&#46;&#46;&#46;</p>
<p>The post <a href="https://www.helpmestudybro.com/print-all-root-to-leaf-paths-in-a-binary-tree/">Print all Root to Leaf Paths in a Binary Tree</a> appeared first on <a href="https://www.helpmestudybro.com">Helpmestudybro</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>“<strong><em>Print all Root to Leaf Paths in a Binary Tree</em></strong>” is an important algorithmic problem based on tree data. Here, we are given a binary tree and our task is to write a program to print all root to leaf paths in given binary tree.</p>


<div class="wp-block-image">
<figure class="aligncenter size-large"><img decoding="async" width="401" height="391" src="https://www.helpmestudybro.com/wp-content/uploads/2021/01/RootToLeafPaths.png" alt="Print all Root to Leaf Paths in a Binary Tree" class="wp-image-1249" srcset="https://www.helpmestudybro.com/wp-content/uploads/2021/01/RootToLeafPaths.png 401w, https://www.helpmestudybro.com/wp-content/uploads/2021/01/RootToLeafPaths-300x293.png 300w, https://www.helpmestudybro.com/wp-content/uploads/2021/01/RootToLeafPaths-50x50.png 50w" sizes="(max-width: 401px) 100vw, 401px" /></figure></div>


<p>Here, the idea is to traverse the given binary tree in preorder fashion and push every encountered node in current path from root to leaf in a vector.</p>



<p>If encountered node is leaf node, we will print the whole vector.</p>



<p>After that, pop out current node from vector after left and right subtree are traversed.</p>



<h4 class="wp-block-heading">C++ Program to print all root to leaf paths in a binary tree is as follows:</h4>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: cpp; title: ; notranslate">
/* C++ Program to Print All Root to Leaf Path in a Binary Tree */
#include&lt;bits/stdc++.h&gt;
using namespace std;

typedef struct Node
{
    int data;
    struct Node *left;
    struct Node *right;
    Node(int ele)
    {
        data = ele;
        left = NULL;
        right = NULL;
    }
} Node;

/* Function to Print All Root to Leaf Path in a Binary Tree */
void printPaths(Node *root, vector&lt;int&gt; &amp;vc)
{
    /* Base Condition: Empty Tree */
    if(root == NULL)
    return;
    
    /* Push Node in current Path Vector */
    vc.push_back(root -&gt; data);
    
    /* If current Node is Leaf Node, we&#039;ll print the Path */
    if(root -&gt; left == NULL &amp;&amp; root -&gt; right == NULL)
    {
        for(int i = 0; i &lt; vc.size(); i++)
        cout&lt;&lt;vc&#x5B;i]&lt;&lt;&quot; &quot;;
        
        cout&lt;&lt;endl;
    }
    
    /* Recur for Left and Right Subtree */
    printPaths(root -&gt; left, vc);
    printPaths(root -&gt; right, vc);
    
    /* Pop Out Current Node after left and right subtree are done */
    vc.pop_back();
}

/* Function to Print All Root to Leaf Path in a Binary Tree */
void printRootToLeaf(Node *root)
{
    /* Base Condition: Empty Tree */
    if(root == NULL)
    return;
    
    vector&lt;int&gt; vc;
    printPaths(root,vc);
}

int main()
{
    /* Creating a Binary trees and inserting some nodes in it */
    Node *root = NULL;
    
    root = new Node(1);
    root -&gt; left = new Node(2);
    root -&gt; right = new Node(3);
    root -&gt; left -&gt; left = new Node(4);
    root -&gt; left -&gt; right = new Node(5);
    root -&gt; right -&gt; left = new Node(6);
    root -&gt; right -&gt; right = new Node (7);
    root -&gt; left -&gt; right -&gt; left = new Node(8);
    root -&gt; right -&gt; left -&gt; right = new Node(9);
    
    /* Calling function to Print All Root to Leaf Path in a Binary Tree */
    printRootToLeaf(root);
}
</pre></div>


<p><strong>OUTPUT:</strong></p>



<pre class="wp-block-preformatted">1 2 4&nbsp;
1 2 5 8&nbsp;
1 3 6 9&nbsp;
1 3 7</pre>



<hr class="wp-block-separator has-css-opacity is-style-wide"/>



<p><strong>Related Posts:</strong></p>



<ol type="1"><li><a href="https://www.helpmestudybro.com/introduction-to-tree-data-structure/"><strong>Introduction to Tree Data Structure</strong></a></li><li><a href="https://www.helpmestudybro.com/binary-tree-traversal/"><strong>Binary Tree Traversals</strong></a></li><li><a href="https://www.helpmestudybro.com/print-all-leaf-nodes-of-a-binary-tree/"><strong>Print All Leaf Nodes of a Binary Tree</strong></a></li><li><a href="https://www.helpmestudybro.com/count-number-of-nodes-in-a-binary-tree/"><strong>Count Number of Nodes in a Binary Tree</strong></a></li><li><a href="https://www.helpmestudybro.com/print-alternate-levels-of-a-binary-tree/"><strong>Print Alternate Levels of Binary Tree</strong></a></li><li><a href="https://www.helpmestudybro.com/maximum-width-of-binary-tree/"><strong>Maximum Width of Binary Tree</strong></a></li><li><a href="https://www.helpmestudybro.com/level-order-tree-traversal/"><strong>Level Order Tree Traversal</strong></a></li><li><a href="https://www.helpmestudybro.com/left-view-of-binary-tree/"><strong>Left View of Binary Tree</strong></a></li><li><a href="https://www.helpmestudybro.com/right-view-of-binary-tree/"><strong>Right View of Binary Tree</strong></a></li><li><a href="https://www.helpmestudybro.com/compute-height-of-a-binary-tree/"><strong>Compute Height of Binary Tree</strong></a></li><li><a href="https://www.helpmestudybro.com/inorder-tree-traversal-using-stack/"><strong>Inorder Tree Traversal Using Stack</strong></a></li><li><a href="https://www.helpmestudybro.com/preorder-traversal-of-binary-tree-using-stack/"><strong>Preorder Tree Trasversal Using Stack</strong></a></li><li><a href="https://www.helpmestudybro.com/postorder-traversal-of-binary-tree-using-stacks/"><strong>Postorder Tree Traversal Using Stack</strong></a></li><li><a href="https://www.helpmestudybro.com/vertical-traversal-of-binary-tree/"><strong>Vertical Order Tree Traversal</strong></a></li><li><a href="https://www.helpmestudybro.com/top-view-of-binary-tree/"><strong>Top View of Binary Tree</strong></a></li><li><a href="https://www.helpmestudybro.com/bottom-view-of-binary-tree/"><strong>Bottom View of Binary Tree</strong></a></li><li><a href="https://www.helpmestudybro.com/delete-binary-tree/"><strong>Delete Complete Binary Tree</strong></a></li><li><a href="https://www.helpmestudybro.com/check-if-two-trees-are-mirror-tree-of-each-other/"><strong>Check if two trees are mirror Trees of Each Other</strong></a></li><li><a href="https://www.helpmestudybro.com/convert-binary-tree-to-its-mirror-tree/"><strong>Convert Binary Tree to its Mirror Tree</strong></a></li><li><a href="https://www.helpmestudybro.com/check-if-binary-tree-is-symmetric-or-not/"><strong>Check if Binary Tree is Symmetric or Not</strong></a></li></ol>



<hr class="wp-block-separator has-css-opacity is-style-wide"/>



<p></p>
<p>The post <a href="https://www.helpmestudybro.com/print-all-root-to-leaf-paths-in-a-binary-tree/">Print all Root to Leaf Paths in a Binary Tree</a> appeared first on <a href="https://www.helpmestudybro.com">Helpmestudybro</a>.</p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>Convert Binary Tree to its Mirror Tree</title>
		<link>https://www.helpmestudybro.com/convert-binary-tree-to-its-mirror-tree/</link>
		
		<dc:creator><![CDATA[admin]]></dc:creator>
		<pubDate>Sun, 24 Jan 2021 18:55:24 +0000</pubDate>
				<category><![CDATA[Binary Tree]]></category>
		<category><![CDATA[data structure]]></category>
		<guid isPermaLink="false">https://www.helpmestudybro.com/?p=1245</guid>

					<description><![CDATA[<p>“Convert Binary Tree to its Mirror Tree” is a basic problem of tree data structure. Here, we are given a binary tree and our task is to write a program to convert given binary tree to its mirror tree. This is a simple problem and can be solved using various&#46;&#46;&#46;</p>
<p>The post <a href="https://www.helpmestudybro.com/convert-binary-tree-to-its-mirror-tree/">Convert Binary Tree to its Mirror Tree</a> appeared first on <a href="https://www.helpmestudybro.com">Helpmestudybro</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>“<strong>Convert Binary Tree to its Mirror Tree</strong>” is a basic problem of tree data structure. Here, we are given a binary tree and our task is to write a program to convert given binary tree to its mirror tree.</p>


<div class="wp-block-image">
<figure class="aligncenter size-large"><img loading="lazy" decoding="async" width="593" height="322" src="https://www.helpmestudybro.com/wp-content/uploads/2021/01/MirrorTrees.png" alt="Check if two trees are mirror tree of each other" class="wp-image-1235" srcset="https://www.helpmestudybro.com/wp-content/uploads/2021/01/MirrorTrees.png 593w, https://www.helpmestudybro.com/wp-content/uploads/2021/01/MirrorTrees-300x163.png 300w" sizes="(max-width: 593px) 100vw, 593px" /><figcaption>Mirror Trees</figcaption></figure></div>


<p>This is a simple problem and can be solved using various traversal algorithm. Here, we are going to solve the given problem using postorder traversal and level order traversal. In both the traversal, we only need to swap the left and right child.</p>



<p><strong>Method 1: Using Postorder Traversal</strong></p>



<h4 class="wp-block-heading">C++ Program to convert binary tree to its mirror tree is as follows:</h4>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: cpp; title: ; notranslate">
/* C++ Program to Convert Binary Tree to its Mirror Tree */
#include&lt;bits/stdc++.h&gt;
using namespace std;

typedef struct Node
{
    int data;
    struct Node *left;
    struct Node *right;
    Node(int ele)
    {
        data = ele;
        left = NULL;
        right = NULL;
    }
} Node;

/* Function to Convert Binary Tree to its Mirror Tree */
void convertMirror(Node *root)
{
    /* Base Condition: Empty Tree */
    if(root == NULL)
    return;
    
    convertMirror(root -&gt; left);
    convertMirror(root -&gt; right);
    
    swap(root -&gt; left, root -&gt; right);
}

/* Inorder Traversal */
void inorder(Node *root)
{
    if(root == NULL)
    return;
    
    inorder(root -&gt; left);
    cout&lt;&lt;root -&gt; data&lt;&lt;&quot; &quot;;
    inorder(root -&gt; right);
}

int main()
{
    /* Creating a Binary trees and inserting some nodes in it */
    Node *root = NULL;
    
    root = new Node(1);
    root -&gt; left = new Node(2);
    root -&gt; right = new Node(3);
    root -&gt; left -&gt; left = new Node(4);
    root -&gt; left -&gt; right = new Node(5);
    root -&gt; right -&gt; left = new Node(6);
    root -&gt; right -&gt; right = new Node (7);
    root -&gt; left -&gt; right -&gt; left = new Node(8);
    root -&gt; right -&gt; left -&gt; right = new Node(9);
    
    /* Printing Inorder Traversal before converting to Mirror Tree */
    cout&lt;&lt;&quot;Inorder Traversal of Original Tree:\n&quot;;
    inorder(root);
    
    /* Calling function to Convert Binary Tree to its Mirror Tree */
    convertMirror(root);
    
    /* Printing Inorder Traversal after converting to Mirror Tree */
    cout&lt;&lt;&quot;\nInorder Traversal of Mirror Tree:\n&quot;;
    inorder(root);
}

</pre></div>


<p><strong>OUTPUT:</strong></p>



<pre class="wp-block-preformatted">Inorder Traversal of Original Tree:
4 2 8 5 1 6 9 3 7&nbsp;
Inorder Traversal of Mirror Tree:
7 3 9 6 1 5 8 2 4</pre>



<p><strong>Method 2: Using Level Order Traversal</strong></p>



<h4 class="wp-block-heading">C++ Program to convert binary tree to its mirror tree is as follows:</h4>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: cpp; title: ; notranslate">
/* C++ Program to Convert Binary Tree to its Mirror Tree */
#include&lt;bits/stdc++.h&gt;
using namespace std;

typedef struct Node
{
    int data;
    struct Node *left;
    struct Node *right;
    Node(int ele)
    {
        data = ele;
        left = NULL;
        right = NULL;
    }
} Node;

/* Function to Convert Binary Tree to its Mirror Tree */
void convertMirror(Node *root)
{
    /* Base Condition: Empty Tree */
    if(root == NULL)
    return;
    
    queue&lt;Node *&gt; qu;
    qu.push(root);
    
    while(!qu.empty())
    {
        Node *curr = qu.front();
        qu.pop();
        
        swap(curr -&gt; left, curr -&gt; right);
        
        if(curr -&gt; left)
        qu.push(curr -&gt; left);
        
        if(curr -&gt; right)
        qu.push(curr -&gt; right);
    }
}

/* Inorder Traversal */
void inorder(Node *root)
{
    if(root == NULL)
    return;
    
    inorder(root -&gt; left);
    cout&lt;&lt;root -&gt; data&lt;&lt;&quot; &quot;;
    inorder(root -&gt; right);
}

int main()
{
    /* Creating a Binary trees and inserting some nodes in it */
    Node *root = NULL;
    
    root = new Node(1);
    root -&gt; left = new Node(2);
    root -&gt; right = new Node(3);
    root -&gt; left -&gt; left = new Node(4);
    root -&gt; left -&gt; right = new Node(5);
    root -&gt; right -&gt; left = new Node(6);
    root -&gt; right -&gt; right = new Node (7);
    root -&gt; left -&gt; right -&gt; left = new Node(8);
    root -&gt; right -&gt; left -&gt; right = new Node(9);
    
    /* Printing Inorder Traversal before converting to Mirror Tree */
    cout&lt;&lt;&quot;Inorder Traversal of Original Tree:\n&quot;;
    inorder(root);
    
    /* Calling function to Convert Binary Tree to its Mirror Tree */
    convertMirror(root);
    
    /* Printing Inorder Traversal after converting to Mirror Tree */
    cout&lt;&lt;&quot;\nInorder Traversal of Mirror Tree:\n&quot;;
    inorder(root);
}
</pre></div>


<p><strong>OUTPUT:</strong></p>



<pre class="wp-block-preformatted">Inorder Traversal of Original Tree:
4 2 8 5 1 6 9 3 7&nbsp;
Inorder Traversal of Mirror Tree:
7 3 9 6 1 5 8 2 4</pre>



<hr class="wp-block-separator has-css-opacity is-style-wide"/>



<p><strong>Related Posts:</strong></p>



<ol type="1"><li><a href="https://www.helpmestudybro.com/check-if-binary-tree-is-symmetric-or-not/"><strong>Check if Binary Tree is Symmetric or Not</strong></a></li><li><a href="https://www.helpmestudybro.com/print-all-root-to-leaf-paths-in-a-binary-tree/"><strong>Print All Root to Leaf Paths in a Binary Tree</strong></a></li><li><a href="https://www.helpmestudybro.com/introduction-to-tree-data-structure/"><strong>Introduction to Tree Data Structure</strong></a></li><li><a href="https://www.helpmestudybro.com/binary-tree-traversal/"><strong>Binary Tree Traversals</strong></a></li><li><a href="https://www.helpmestudybro.com/print-all-leaf-nodes-of-a-binary-tree/"><strong>Print All Leaf Nodes of a Binary Tree</strong></a></li><li><a href="https://www.helpmestudybro.com/count-number-of-nodes-in-a-binary-tree/"><strong>Count Number of Nodes in a Binary Tree</strong></a></li><li><a href="https://www.helpmestudybro.com/print-alternate-levels-of-a-binary-tree/"><strong>Print Alternate Levels of Binary Tree</strong></a></li><li><a href="https://www.helpmestudybro.com/maximum-width-of-binary-tree/"><strong>Maximum Width of Binary Tree</strong></a></li><li><a href="https://www.helpmestudybro.com/level-order-tree-traversal/"><strong>Level Order Tree Traversal</strong></a></li><li><a href="https://www.helpmestudybro.com/left-view-of-binary-tree/"><strong>Left View of Binary Tree</strong></a></li><li><a href="https://www.helpmestudybro.com/right-view-of-binary-tree/"><strong>Right View of Binary Tree</strong></a></li><li><a href="https://www.helpmestudybro.com/compute-height-of-a-binary-tree/"><strong>Compute Height of Binary Tree</strong></a></li><li><a href="https://www.helpmestudybro.com/inorder-tree-traversal-using-stack/"><strong>Inorder Tree Traversal Using Stack</strong></a></li><li><a href="https://www.helpmestudybro.com/preorder-traversal-of-binary-tree-using-stack/"><strong>Preorder Tree Trasversal Using Stack</strong></a></li><li><a href="https://www.helpmestudybro.com/postorder-traversal-of-binary-tree-using-stacks/"><strong>Postorder Tree Traversal Using Stack</strong></a></li><li><a href="https://www.helpmestudybro.com/vertical-traversal-of-binary-tree/"><strong>Vertical Order Tree Traversal</strong></a></li><li><a href="https://www.helpmestudybro.com/top-view-of-binary-tree/"><strong>Top View of Binary Tree</strong></a></li><li><a href="https://www.helpmestudybro.com/bottom-view-of-binary-tree/"><strong>Bottom View of Binary Tree</strong></a></li><li><a href="https://www.helpmestudybro.com/delete-binary-tree/"><strong>Delete Complete Binary Tree</strong></a></li><li><a href="https://www.helpmestudybro.com/check-if-two-trees-are-mirror-tree-of-each-other/"><strong>Check if two trees are mirror Trees of Each Other</strong></a></li></ol>



<hr class="wp-block-separator has-css-opacity is-style-wide"/>



<p></p>
<p>The post <a href="https://www.helpmestudybro.com/convert-binary-tree-to-its-mirror-tree/">Convert Binary Tree to its Mirror Tree</a> appeared first on <a href="https://www.helpmestudybro.com">Helpmestudybro</a>.</p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>Check if Binary Tree is Symmetric or Not</title>
		<link>https://www.helpmestudybro.com/check-if-binary-tree-is-symmetric-or-not/</link>
		
		<dc:creator><![CDATA[admin]]></dc:creator>
		<pubDate>Sun, 24 Jan 2021 10:16:24 +0000</pubDate>
				<category><![CDATA[Binary Tree]]></category>
		<category><![CDATA[data structure]]></category>
		<guid isPermaLink="false">https://www.helpmestudybro.com/?p=1241</guid>

					<description><![CDATA[<p>“Check if Binary Tree is Symmetric or Not” is a basic problem based on tree data structure. Here, we are given a binary tree and our task is to write a program to check if given binary tree have symmetric structure or not. A Binary Tree is having symmetric structure&#46;&#46;&#46;</p>
<p>The post <a href="https://www.helpmestudybro.com/check-if-binary-tree-is-symmetric-or-not/">Check if Binary Tree is Symmetric or Not</a> appeared first on <a href="https://www.helpmestudybro.com">Helpmestudybro</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>“<strong><em>Check if Binary Tree is Symmetric or Not</em></strong>” is a basic problem based on tree data structure. Here, we are given a binary tree and our task is to write a program to check if given binary tree have symmetric structure or not.</p>



<p>A Binary Tree is having symmetric structure if left subtree and right subtree are mirror image of each other, irrespective of node value.</p>


<div class="wp-block-image">
<figure class="aligncenter size-large"><img loading="lazy" decoding="async" width="401" height="321" src="https://www.helpmestudybro.com/wp-content/uploads/2021/01/SymmetricTree.png" alt="Check if Binary Tree is Symmetric or Not" class="wp-image-1242" srcset="https://www.helpmestudybro.com/wp-content/uploads/2021/01/SymmetricTree.png 401w, https://www.helpmestudybro.com/wp-content/uploads/2021/01/SymmetricTree-300x240.png 300w" sizes="(max-width: 401px) 100vw, 401px" /><figcaption>Symmetric Tree</figcaption></figure></div>


<p>We have already discussed to check if two binary trees are mirror tree of each other or not. Here, also, we will use same technique to check symmetry of tree.</p>



<p>A Binary Tree is Symmetric if it holds following conditions:</p>



<ol type="1"><li>Both Left subtree and right subtree are empty or non-empty.</li><li>Left Subtree is mirror image of right subtree, irrespective of data.</li><li>Right Subtree is mirror image of left subtree, irrespective of data.</li></ol>



<h4 class="wp-block-heading">C++ Program to Check if Binary Tree is Symmetric or Not is as follows:</h4>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: cpp; title: ; notranslate">
/* C++ Program to Check if Binary Tree is Symmetric or Not */
#include&lt;bits/stdc++.h&gt;
using namespace std;

typedef struct Node
{
    int data;
    struct Node *left;
    struct Node *right;
    Node(int ele)
    {
        data = ele;
        left = NULL;
        right = NULL;
    }
} Node;

/* Function to Check if Binary Tree is Symmetric or Not */
bool checkSymmetry(Node *root1, Node *root2)
{
    /* Base Case1: If both Subtrees are Empty */
    if(root1 == NULL &amp;&amp; root2 == NULL)
    return true;
    
    /* Base Case2: If one subtree is empty and other not */
    if(root1 == NULL || root2 == NULL)
    return false;
    
    /*
        If Both are Non-Empty, Recursively compare left subtree 
         with right subtree and vice-versa
    */
    return (root1 != NULL &amp;&amp; root2 != NULL) &amp;&amp;
           checkSymmetry(root1 -&gt; left, root2 -&gt; right) &amp;&amp;
           checkSymmetry(root1 -&gt; right,root2 -&gt; left);
}

int main()
{
    /* Creating a Binary trees and inserting some nodes in it */
    Node *root = NULL;
    
    root = new Node(1);
    root -&gt; left = new Node(2);
    root -&gt; right = new Node(3);
    root -&gt; left -&gt; left = new Node(4);
    root -&gt; left -&gt; right = new Node(5);
    root -&gt; right -&gt; left = new Node(6);
    root -&gt; right -&gt; right = new Node (7);
    root -&gt; left -&gt; right -&gt; left = new Node(8);
    root -&gt; right -&gt; left -&gt; right = new Node(9);
    
    /* Calling function to Check if Binary Tree is Symmetric or Not */
    if(checkSymmetry(root -&gt; left, root -&gt; right) == true)
    {
        cout&lt;&lt;&quot;Given Binary Tree is Symmetric Tree!!&quot;;
    }
    else
    {
        cout&lt;&lt;&quot;Given Binary Tree is not Symmetric Tree!!&quot;;
    }
}

</pre></div>


<p><strong>OUTPUT:</strong></p>



<pre class="wp-block-preformatted">Given Binary Tree is Symmetric Tree!!</pre>



<hr class="wp-block-separator has-css-opacity is-style-wide"/>



<p><strong>Related Posts:</strong></p>



<ol type="1"><li><a href="https://www.helpmestudybro.com/check-if-two-trees-are-mirror-tree-of-each-other/"><strong>Check if two trees are mirror Trees of Each Other</strong></a></li><li><a href="https://www.helpmestudybro.com/convert-binary-tree-to-its-mirror-tree/"><strong>Convert Binary Tree to its Mirror Tree</strong></a></li><li><a href="https://www.helpmestudybro.com/introduction-to-tree-data-structure/"><strong>Introduction to Tree Data Structure</strong></a></li><li><a href="https://www.helpmestudybro.com/binary-tree-traversal/"><strong>Binary Tree Traversals</strong></a></li><li><a href="https://www.helpmestudybro.com/print-all-leaf-nodes-of-a-binary-tree/"><strong>Print All Leaf Nodes of a Binary Tree</strong></a></li><li><a href="https://www.helpmestudybro.com/count-number-of-nodes-in-a-binary-tree/"><strong>Count Number of Nodes in a Binary Tree</strong></a></li><li><a href="https://www.helpmestudybro.com/print-alternate-levels-of-a-binary-tree/"><strong>Print Alternate Levels of Binary Tree</strong></a></li><li><a href="https://www.helpmestudybro.com/maximum-width-of-binary-tree/"><strong>Maximum Width of Binary Tree</strong></a></li><li><a href="https://www.helpmestudybro.com/level-order-tree-traversal/"><strong>Level Order Tree Traversal</strong></a></li><li><a href="https://www.helpmestudybro.com/left-view-of-binary-tree/"><strong>Left View of Binary Tree</strong></a></li><li><a href="https://www.helpmestudybro.com/right-view-of-binary-tree/"><strong>Right View of Binary Tree</strong></a></li><li><a href="https://www.helpmestudybro.com/compute-height-of-a-binary-tree/"><strong>Compute Height of Binary Tree</strong></a></li><li><a href="https://www.helpmestudybro.com/inorder-tree-traversal-using-stack/"><strong>Inorder Tree Traversal Using Stack</strong></a></li><li><a href="https://www.helpmestudybro.com/preorder-traversal-of-binary-tree-using-stack/"><strong>Preorder Tree Trasversal Using Stack</strong></a></li><li><a href="https://www.helpmestudybro.com/postorder-traversal-of-binary-tree-using-stacks/"><strong>Postorder Tree Traversal Using Stack</strong></a></li><li><a href="https://www.helpmestudybro.com/vertical-traversal-of-binary-tree/"><strong>Vertical Order Tree Traversal</strong></a></li><li><a href="https://www.helpmestudybro.com/top-view-of-binary-tree/"><strong>Top View of Binary Tree</strong></a></li><li><a href="https://www.helpmestudybro.com/bottom-view-of-binary-tree/"><strong>Bottom View of Binary Tree</strong></a></li><li><a href="https://www.helpmestudybro.com/delete-binary-tree/"><strong>Delete Complete Binary Tree</strong></a></li><li><a href="https://www.helpmestudybro.com/print-all-root-to-leaf-paths-in-a-binary-tree/"><strong>Print All Root to Leaf Paths in a Binary Tree</strong></a></li></ol>



<hr class="wp-block-separator has-css-opacity is-style-wide"/>



<p></p>
<p>The post <a href="https://www.helpmestudybro.com/check-if-binary-tree-is-symmetric-or-not/">Check if Binary Tree is Symmetric or Not</a> appeared first on <a href="https://www.helpmestudybro.com">Helpmestudybro</a>.</p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>Delete Binary Tree</title>
		<link>https://www.helpmestudybro.com/delete-binary-tree/</link>
		
		<dc:creator><![CDATA[admin]]></dc:creator>
		<pubDate>Fri, 22 Jan 2021 20:48:00 +0000</pubDate>
				<category><![CDATA[Binary Tree]]></category>
		<category><![CDATA[data structure]]></category>
		<guid isPermaLink="false">https://www.helpmestudybro.com/?p=1238</guid>

					<description><![CDATA[<p>“Delete Binary Tree” is an elementary problem based on tree data structure. Here, we are given a binary tree and our task is to delete given binary tree. We need to delete all the nodes and release memory. If tree needs to be deleted, then, it needs to be traversed&#46;&#46;&#46;</p>
<p>The post <a href="https://www.helpmestudybro.com/delete-binary-tree/">Delete Binary Tree</a> appeared first on <a href="https://www.helpmestudybro.com">Helpmestudybro</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>“<strong><em>Delete Binary Tree</em></strong>” is an elementary problem based on tree data structure. Here, we are given a binary tree and our task is to delete given binary tree. We need to delete all the nodes and release memory.</p>



<p>If tree needs to be deleted, then, it needs to be traversed first and one-by-one memory of all the nodes must be released. It’s obvious that before deleting any parent node, we need to delete child node. So, for that, postorder traversal can be used, as it will help to delete left child, then right child and in last root node.</p>



<h4 class="wp-block-heading">C++ Program to Delete Binary Tree is as follows:</h4>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: cpp; title: ; notranslate">
/* C++ Program to Delete Binary Tree */
#include&lt;bits/stdc++.h&gt;
using namespace std;

typedef struct Node
{
    int data;
    struct Node *left;
    struct Node *right;
    Node(int ele)
    {
        data = ele;
        left = NULL;
        right = NULL;
    }
} Node;

/* Function to Delete Binary Tree */
void deleteTree(Node *root)
{
   /* Base Case */
   if(root == NULL)
   return;
   
   deleteTree(root -&gt; left);
   deleteTree(root -&gt; right);
   
   cout&lt;&lt;&quot;Deleting Node with Value &quot;&lt;&lt;root-&gt;data&lt;&lt;&quot;\n&quot;;
   delete root;
}

int main()
{
    /* Creating a Binary trees and inserting some nodes in it */
    Node *root = NULL;
    
    root = new Node(1);
    root -&gt; left = new Node(2);
    root -&gt; right = new Node(3);
    root -&gt; left -&gt; left = new Node(4);
    root -&gt; left -&gt; right = new Node(5);
    root -&gt; right -&gt; left = new Node(6);
    root -&gt; right -&gt; right = new Node (7);
    root -&gt; left -&gt; right -&gt; left = new Node(8);
    root -&gt; right -&gt; left -&gt; right = new Node(9);
    
    /* Calling function to delete binary tree */
    deleteTree(root);
}
</pre></div>


<p><strong>OUTPUT:</strong></p>



<pre class="wp-block-preformatted">Deleting Node with Value 4
Deleting Node with Value 8
Deleting Node with Value 5
Deleting Node with Value 2
Deleting Node with Value 9
Deleting Node with Value 6
Deleting Node with Value 7
Deleting Node with Value 3
Deleting Node with Value 1</pre>



<p><strong>Method 2: Deletion using Level Order Traversal</strong></p>



<p>We can delete the given binary tree using level order traversal also. In the previous method, we delete the leaf nodes first and then their parents. But, here, we will delete the tree in root to leaf fashion.</p>



<p>Before deleting any node, we will push the left and right child of it, if exist, into queue.</p>



<h4 class="wp-block-heading">C++ Program to Delete Binary Tree is as follows:</h4>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: cpp; title: ; notranslate">
/* C++ Program to Delete Binary Tree */
#include&lt;bits/stdc++.h&gt;
using namespace std;

typedef struct Node
{
    int data;
    struct Node *left;
    struct Node *right;
    Node(int ele)
    {
        data = ele;
        left = NULL;
        right = NULL;
    }
} Node;

/* Function to Delete Binary Tree */
void deleteTree(Node *root)
{
   /* Base Case */
   if(root == NULL)
   return;
   
   queue&lt;Node *&gt; que;
   
   que.push(root);
   
   while(!que.empty())
   {
       Node *temp = que.front();
       que.pop();
       
       if(temp -&gt; left)
       que.push(temp -&gt; left);
       
       if(temp -&gt; right)
       que.push(temp -&gt; right);
       
       cout&lt;&lt;&quot;Deleting Node with Value &quot;&lt;&lt;temp -&gt; data&lt;&lt;&quot;\n&quot;;
       delete temp;
   }
   
   root = NULL;
}

int main()
{
    /* Creating a Binary trees and inserting some nodes in it */
    Node *root = NULL;
    
    root = new Node(1);
    root -&gt; left = new Node(2);
    root -&gt; right = new Node(3);
    root -&gt; left -&gt; left = new Node(4);
    root -&gt; left -&gt; right = new Node(5);
    root -&gt; right -&gt; left = new Node(6);
    root -&gt; right -&gt; right = new Node (7);
    root -&gt; left -&gt; right -&gt; left = new Node(8);
    root -&gt; right -&gt; left -&gt; right = new Node(9);
    
    /* Calling function to delete binary tree */
    deleteTree(root);
}
</pre></div>


<p><strong>OUTPUT:</strong></p>



<pre class="wp-block-preformatted">Deleting Node with Value 1
Deleting Node with Value 2
Deleting Node with Value 3
Deleting Node with Value 4
Deleting Node with Value 5
Deleting Node with Value 6
Deleting Node with Value 7
Deleting Node with Value 8
Deleting Node with Value 9</pre>



<hr class="wp-block-separator is-style-wide"/>



<p><strong>Related Posts:</strong></p>



<ol type="1"><li><a href="https://www.helpmestudybro.com/introduction-to-tree-data-structure/"><strong>Introduction to Tree Data Structure</strong></a></li><li><a href="https://www.helpmestudybro.com/binary-tree-traversal/"><strong>Binary Tree Traversals</strong></a></li><li><a href="https://www.helpmestudybro.com/print-all-leaf-nodes-of-a-binary-tree/"><strong>Print All Leaf Nodes of a Binary Tree</strong></a></li><li><a href="https://www.helpmestudybro.com/count-number-of-nodes-in-a-binary-tree/"><strong>Count Number of Nodes in a Binary Tree</strong></a></li><li><a href="https://www.helpmestudybro.com/print-alternate-levels-of-a-binary-tree/"><strong>Print Alternate Levels of Binary Tree</strong></a></li><li><a href="https://www.helpmestudybro.com/maximum-width-of-binary-tree/"><strong>Maximum Width of Binary Tree</strong></a></li><li><a href="https://www.helpmestudybro.com/level-order-tree-traversal/"><strong>Level Order Tree Traversal</strong></a></li><li><a href="https://www.helpmestudybro.com/left-view-of-binary-tree/"><strong>Left View of Binary Tree</strong></a></li><li><a href="https://www.helpmestudybro.com/right-view-of-binary-tree/"><strong>Right View of Binary Tree</strong></a></li><li><a href="https://www.helpmestudybro.com/compute-height-of-a-binary-tree/"><strong>Compute Height of Binary Tree</strong></a></li><li><a href="https://www.helpmestudybro.com/inorder-tree-traversal-using-stack/"><strong>Inorder Tree Traversal Using Stack</strong></a></li><li><a href="https://www.helpmestudybro.com/preorder-traversal-of-binary-tree-using-stack/"><strong>Preorder Tree Trasversal Using Stack</strong></a></li><li><a href="https://www.helpmestudybro.com/postorder-traversal-of-binary-tree-using-stacks/"><strong>Postorder Tree Traversal Using Stack</strong></a></li><li><a href="https://www.helpmestudybro.com/vertical-traversal-of-binary-tree/"><strong>Vertical Order Tree Traversal</strong></a></li><li><a href="https://www.helpmestudybro.com/top-view-of-binary-tree/"><strong>Top View of Binary Tree</strong></a></li><li><a href="https://www.helpmestudybro.com/bottom-view-of-binary-tree/"><strong>Bottom View of Binary Tree</strong></a></li><li><a href="https://www.helpmestudybro.com/check-if-two-trees-are-mirror-tree-of-each-other/"><strong>Check if two trees are mirror Trees of Each Other</strong></a></li><li><a href="https://www.helpmestudybro.com/convert-binary-tree-to-its-mirror-tree/"><strong>Convert Binary Tree to its Mirror Tree</strong></a></li><li><a href="https://www.helpmestudybro.com/check-if-binary-tree-is-symmetric-or-not/"><strong>Check if Binary Tree is Symmetric or Not</strong></a></li><li><a href="https://www.helpmestudybro.com/print-all-root-to-leaf-paths-in-a-binary-tree/"><strong>Print All Root to Leaf Paths in a Binary Tree</strong></a></li></ol>



<hr class="wp-block-separator is-style-wide"/>



<p></p>
<p>The post <a href="https://www.helpmestudybro.com/delete-binary-tree/">Delete Binary Tree</a> appeared first on <a href="https://www.helpmestudybro.com">Helpmestudybro</a>.</p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>Check if two trees are mirror tree of each other</title>
		<link>https://www.helpmestudybro.com/check-if-two-trees-are-mirror-tree-of-each-other/</link>
		
		<dc:creator><![CDATA[admin]]></dc:creator>
		<pubDate>Fri, 22 Jan 2021 20:37:42 +0000</pubDate>
				<category><![CDATA[Binary Tree]]></category>
		<category><![CDATA[data structure]]></category>
		<guid isPermaLink="false">https://www.helpmestudybro.com/?p=1234</guid>

					<description><![CDATA[<p>“Check if two trees are mirror tree of each other” is a basic problem based on tree data structure. Here, we are given two binary tree and our task is to write a program to check whether given two binary trees are mirror tree of each other. Two trees are&#46;&#46;&#46;</p>
<p>The post <a href="https://www.helpmestudybro.com/check-if-two-trees-are-mirror-tree-of-each-other/">Check if two trees are mirror tree of each other</a> appeared first on <a href="https://www.helpmestudybro.com">Helpmestudybro</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>“<strong><em>Check if two trees are mirror tree of each other</em></strong>” is a basic problem based on tree data structure. Here, we are given two binary tree and our task is to write a program to check whether given two binary trees are mirror tree of each other.</p>



<div class="wp-block-image"><figure class="aligncenter size-large"><img loading="lazy" decoding="async" width="593" height="322" src="https://www.helpmestudybro.com/wp-content/uploads/2021/01/MirrorTrees.png" alt="Check if two trees are mirror tree of each other" class="wp-image-1235" srcset="https://www.helpmestudybro.com/wp-content/uploads/2021/01/MirrorTrees.png 593w, https://www.helpmestudybro.com/wp-content/uploads/2021/01/MirrorTrees-300x163.png 300w" sizes="(max-width: 593px) 100vw, 593px" /><figcaption>Check if two trees are mirror tree of each other</figcaption></figure></div>



<p>Two trees are known as mirror tree of each other if they satisfy following conditions:</p>



<ol type="1"><li>Both must be empty or non-empty tree.</li><li>Root Node data must be same.</li><li>Left subtree of first tree must be equal to right subtree of second tree.</li><li>Right subtree of first tree must be equal to left subtree of second tree.</li></ol>



<h4 class="wp-block-heading">C++ Program to check if two trees are mirror tree of each other is as follows:</h4>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: cpp; title: ; notranslate">
/* C++ Program to check if two trees are mirror tree of each other */
#include&lt;bits/stdc++.h&gt;
using namespace std;

typedef struct Node
{
    int data;
    struct Node *left;
    struct Node *right;
    Node(int ele)
    {
        data = ele;
        left = NULL;
        right = NULL;
    }
} Node;

/* Function to check if two trees are mirror tree of each other */
bool checkMirror(Node *root1, Node *root2)
{
    /* Base Case1: If both Trees are Empty */
    if(root1 == NULL &amp;&amp; root2 == NULL)
    return true;
    
    /* Base Case2: If one tree is empty and other not */
    if(root1 == NULL || root2 == NULL)
    return false;
    
    /*
        If Both are Non-Empty, Recursively compare data of left subtree of 
        first tree with right subtree of second tree and vice-versa
    */
    return root1 -&gt; data == root2 -&gt; data &amp;&amp;
           checkMirror(root1 -&gt; left, root2 -&gt; right) &amp;&amp;
           checkMirror(root1 -&gt; right,root2 -&gt; left);
}

int main()
{
    /* Creating a Binary trees and inserting some nodes in it */
    Node *root1 = NULL;
    Node *root2 = NULL;
    
    /* Inserting some Nodes in first tree */
    root1 = new Node(1);
    root1 -&gt; left = new Node(2);
    root1 -&gt; right = new Node(3);
    root1 -&gt; left -&gt; left = new Node(4);
    root1 -&gt; left -&gt; right = new Node(5);
    root1 -&gt; right -&gt; left = new Node(6);
    root1 -&gt; right -&gt; right = new Node (7);
    root1 -&gt; left -&gt; right -&gt; left = new Node(8);
    root1 -&gt; right -&gt; left -&gt; right = new Node(9);
    
    /* Inserting some Nodes in second tree */
    root2 = new Node(1);
    root2 -&gt; right = new Node(2);
    root2 -&gt; left = new Node(3);
    root2 -&gt; right -&gt; right = new Node(4);
    root2 -&gt; right -&gt; left = new Node(5);
    root2 -&gt; left -&gt; right = new Node(6);
    root2 -&gt; left -&gt; left = new Node (7);
    root2 -&gt; right -&gt; left -&gt; right = new Node(8);
    root2 -&gt; left -&gt; right -&gt; left = new Node(9);
    
    /* Calling function to check if two trees are mirror tree of each other*/
    if(checkMirror(root1,root2) == true)
    {
        cout&lt;&lt;&quot;Both Trees are Mirror Tree of Each Other!!&quot;;
    }
    else
    {
        cout&lt;&lt;&quot;Both Trees are not Mirror Tree of Each Other!!&quot;;
    }
    
}
</pre></div>


<p><strong>OUTPUT:</strong></p>



<pre class="wp-block-preformatted">Both Trees are Mirror Tree of Each Other!!</pre>



<hr class="wp-block-separator is-style-wide"/>



<p><strong>Related Posts:</strong></p>



<ol type="1"><li><a href="https://www.helpmestudybro.com/convert-binary-tree-to-its-mirror-tree/"><strong>Convert Binary Tree to its Mirror Tree</strong></a></li><li><a href="https://www.helpmestudybro.com/check-if-binary-tree-is-symmetric-or-not/"><strong>Check if Binary Tree is Symmetric or Not</strong></a></li><li><a href="https://www.helpmestudybro.com/print-all-root-to-leaf-paths-in-a-binary-tree/"><strong>Print All Root to Leaf Paths in a Binary Tree</strong></a></li><li><a href="https://www.helpmestudybro.com/introduction-to-tree-data-structure/"><strong>Introduction to Tree Data Structure</strong></a></li><li><a href="https://www.helpmestudybro.com/binary-tree-traversal/"><strong>Binary Tree Traversals</strong></a></li><li><a href="https://www.helpmestudybro.com/print-all-leaf-nodes-of-a-binary-tree/"><strong>Print All Leaf Nodes of a Binary Tree</strong></a></li><li><a href="https://www.helpmestudybro.com/count-number-of-nodes-in-a-binary-tree/"><strong>Count Number of Nodes in a Binary Tree</strong></a></li><li><a href="https://www.helpmestudybro.com/print-alternate-levels-of-a-binary-tree/"><strong>Print Alternate Levels of Binary Tree</strong></a></li><li><a href="https://www.helpmestudybro.com/maximum-width-of-binary-tree/"><strong>Maximum Width of Binary Tree</strong></a></li><li><a href="https://www.helpmestudybro.com/level-order-tree-traversal/"><strong>Level Order Tree Traversal</strong></a></li><li><a href="https://www.helpmestudybro.com/left-view-of-binary-tree/"><strong>Left View of Binary Tree</strong></a></li><li><a href="https://www.helpmestudybro.com/right-view-of-binary-tree/"><strong>Right View of Binary Tree</strong></a></li><li><a href="https://www.helpmestudybro.com/compute-height-of-a-binary-tree/"><strong>Compute Height of Binary Tree</strong></a></li><li><a href="https://www.helpmestudybro.com/inorder-tree-traversal-using-stack/"><strong>Inorder Tree Traversal Using Stack</strong></a></li><li><a href="https://www.helpmestudybro.com/preorder-traversal-of-binary-tree-using-stack/"><strong>Preorder Tree Trasversal Using Stack</strong></a></li><li><a href="https://www.helpmestudybro.com/postorder-traversal-of-binary-tree-using-stacks/"><strong>Postorder Tree Traversal Using Stack</strong></a></li><li><a href="https://www.helpmestudybro.com/vertical-traversal-of-binary-tree/"><strong>Vertical Order Tree Traversal</strong></a></li><li><a href="https://www.helpmestudybro.com/top-view-of-binary-tree/"><strong>Top View of Binary Tree</strong></a></li><li><a href="https://www.helpmestudybro.com/bottom-view-of-binary-tree/"><strong>Bottom View of Binary Tree</strong></a></li><li><a href="https://www.helpmestudybro.com/delete-binary-tree/"><strong>Delete Complete Binary Tree</strong></a></li></ol>



<hr class="wp-block-separator is-style-wide"/>



<p></p>
<p>The post <a href="https://www.helpmestudybro.com/check-if-two-trees-are-mirror-tree-of-each-other/">Check if two trees are mirror tree of each other</a> appeared first on <a href="https://www.helpmestudybro.com">Helpmestudybro</a>.</p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>Top View of Binary Tree</title>
		<link>https://www.helpmestudybro.com/top-view-of-binary-tree/</link>
		
		<dc:creator><![CDATA[admin]]></dc:creator>
		<pubDate>Fri, 22 Jan 2021 20:19:04 +0000</pubDate>
				<category><![CDATA[Binary Tree]]></category>
		<category><![CDATA[data structure]]></category>
		<guid isPermaLink="false">https://www.helpmestudybro.com/?p=1230</guid>

					<description><![CDATA[<p>“Top View of Binary Tree”&#160;is one of the foremost algorithmic problem asked in technical interviews of product-based companies. Here, we are given a binary tree and our task is to write a program to print the top view of given binary tree. Top View of Binary Tree is referred to&#46;&#46;&#46;</p>
<p>The post <a href="https://www.helpmestudybro.com/top-view-of-binary-tree/">Top View of Binary Tree</a> appeared first on <a href="https://www.helpmestudybro.com">Helpmestudybro</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p><strong><em>“Top View of Binary Tree”</em></strong>&nbsp;is one of the foremost algorithmic problem asked in technical interviews of product-based companies. Here, we are given a binary tree and our task is to write a program to print the top view of given binary tree.</p>



<p>Top View of Binary Tree is referred to those nodes of binary tree which can be seen from top of the tree.</p>


<div class="wp-block-image">
<figure class="aligncenter size-large"><img loading="lazy" decoding="async" width="401" height="401" src="https://www.helpmestudybro.com/wp-content/uploads/2021/01/TopView.png" alt="Top View of Binary Tree" class="wp-image-1231" srcset="https://www.helpmestudybro.com/wp-content/uploads/2021/01/TopView.png 401w, https://www.helpmestudybro.com/wp-content/uploads/2021/01/TopView-300x300.png 300w, https://www.helpmestudybro.com/wp-content/uploads/2021/01/TopView-150x150.png 150w, https://www.helpmestudybro.com/wp-content/uploads/2021/01/TopView-80x80.png 80w, https://www.helpmestudybro.com/wp-content/uploads/2021/01/TopView-320x320.png 320w, https://www.helpmestudybro.com/wp-content/uploads/2021/01/TopView-50x50.png 50w" sizes="(max-width: 401px) 100vw, 401px" /><figcaption>Top View of Binary Tree</figcaption></figure></div>


<p>The idea here is similar to vertical order traversal of binary tree. We will perform the vertical order traversal and print only those nodes which are first nodes of vertical order line.</p>



<p>The steps required to print top view of Binary Tree are as follows:</p>



<ol type="1"><li>Create a map which store the vertical order line number and corresponding first/top node value.</li><li>Create a queue which can accommodate both node and corresponding horizontal distance with root node.</li><li>Insert the root node into queue and 0 as horizontal distance.</li><li>Perform following steps until queue is node empty.<ul><li>Pop front Element of queue.</li><li>Check if popped horizontal distance already present in the map or not. If not, insert the popped node value into map.</li><li>If left child exists of popped node, insert into queue with horizontal distance as current HD-1.</li><li>If right child exists of popped node, insert into queue with horizontal distance as current HD+1.</li></ul></li><li>Print the map data which contains top view.</li></ol>



<h4 class="wp-block-heading">C++ Program to print top view of binary tree is as follows:</h4>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: cpp; title: ; notranslate">
/* C++ Program to print top view of Binary Tree */
#include&lt;bits/stdc++.h&gt;
using namespace std;

typedef struct Node
{
    int data;
    struct Node *left;
    struct Node *right;
    Node(int ele)
    {
        data = ele;
        left = NULL;
        right = NULL;
    }
} Node;

/* Function to print top view of Binary Tree */
void topView(Node *root)
{
    /* Base Case */
    if(root == NULL)
    return;
    
    /*
        Create a map which store top view nodes
        key: vertical order line
        pair: Top node of vertical order line
    */
    map&lt;int, int&gt; mp;
    
    /*
        Create queue which store both node and corresponding HD
    */
    queue&lt;pair&lt;Node*, int&gt;&gt; que;
    int hd = 0;
    
    que.push(make_pair(root,hd));
    
    while(!que.empty())
    {
        /* Pop front Node */
        pair&lt;Node *,int&gt; temp = que.front();
        que.pop();
        Node *temp_node = temp.first;
        int currHD = temp.second;
        
        /* 
            If vertical order line is not present in map, 
            then insert the node value in the map, as it will the 
            first node of the verical order line
        */
        if(mp.find(currHD) == mp.end())
        {
            mp&#x5B;currHD] = temp_node -&gt; data;
        }
        
        if(temp_node -&gt; left)
        que.push(make_pair(temp_node -&gt; left, currHD - 1));
        
        if(temp_node -&gt; right)
        que.push(make_pair(temp_node -&gt; right, currHD + 1));
    }
    
    /* Print map, which contain top view nodes */
    map&lt;int, int&gt; :: iterator itr;
    for(itr = mp.begin(); itr != mp.end(); ++itr)
    {
        cout &lt;&lt; itr -&gt; second &lt;&lt; &quot; &quot;;
    }
}

int main()
{
    /* Creating a Binary tree and inserting some nodes in it */
    Node *root = NULL;
    
    root = new Node(1);
    root -&gt; left = new Node(2);
    root -&gt; right = new Node(3);
    root -&gt; left -&gt; left = new Node(4);
    root -&gt; left -&gt; right = new Node(5);
    root -&gt; right -&gt; left = new Node(6);
    root -&gt; right -&gt; right = new Node (7);
    root -&gt; left -&gt; right -&gt; left = new Node(8);
    root -&gt; right -&gt; left -&gt; right = new Node(9);
    
    /* Calling function to print top view of binary tree*/
    cout&lt;&lt;&quot;The top View of Given Binary Tree is:\n&quot;;
    topView(root);
}

</pre></div>


<p><strong>OUTPUT:</strong></p>



<pre class="wp-block-preformatted">The top View of Given Binary Tree is:
4 2 1 3 7</pre>



<hr class="wp-block-separator has-css-opacity is-style-wide"/>



<p><strong>Related Posts:</strong></p>



<ol type="1"><li><a href="https://www.helpmestudybro.com/bottom-view-of-binary-tree/"><strong>Bottom View of Binary Tree</strong></a></li><li><a href="https://www.helpmestudybro.com/delete-binary-tree/"><strong>Delete Complete Binary Tree</strong></a></li><li><a href="https://www.helpmestudybro.com/check-if-two-trees-are-mirror-tree-of-each-other/"><strong>Check if two trees are mirror Trees of Each Other</strong></a></li><li><a href="https://www.helpmestudybro.com/convert-binary-tree-to-its-mirror-tree/"><strong>Convert Binary Tree to its Mirror Tree</strong></a></li><li><a href="https://www.helpmestudybro.com/check-if-binary-tree-is-symmetric-or-not/"><strong>Check if Binary Tree is Symmetric or Not</strong></a></li><li><a href="https://www.helpmestudybro.com/print-all-root-to-leaf-paths-in-a-binary-tree/"><strong>Print All Root to Leaf Paths in a Binary Tree</strong></a></li><li><a href="https://www.helpmestudybro.com/introduction-to-tree-data-structure/"><strong>Introduction to Tree Data Structure</strong></a></li><li><a href="https://www.helpmestudybro.com/binary-tree-traversal/"><strong>Binary Tree Traversals</strong></a></li><li><a href="https://www.helpmestudybro.com/print-all-leaf-nodes-of-a-binary-tree/"><strong>Print All Leaf Nodes of a Binary Tree</strong></a></li><li><a href="https://www.helpmestudybro.com/count-number-of-nodes-in-a-binary-tree/"><strong>Count Number of Nodes in a Binary Tree</strong></a></li><li><a href="https://www.helpmestudybro.com/print-alternate-levels-of-a-binary-tree/"><strong>Print Alternate Levels of Binary Tree</strong></a></li><li><a href="https://www.helpmestudybro.com/maximum-width-of-binary-tree/"><strong>Maximum Width of Binary Tree</strong></a></li><li><a href="https://www.helpmestudybro.com/level-order-tree-traversal/"><strong>Level Order Tree Traversal</strong></a></li><li><a href="https://www.helpmestudybro.com/left-view-of-binary-tree/"><strong>Left View of Binary Tree</strong></a></li><li><a href="https://www.helpmestudybro.com/right-view-of-binary-tree/"><strong>Right View of Binary Tree</strong></a></li><li><a href="https://www.helpmestudybro.com/compute-height-of-a-binary-tree/"><strong>Compute Height of Binary Tree</strong></a></li><li><a href="https://www.helpmestudybro.com/inorder-tree-traversal-using-stack/"><strong>Inorder Tree Traversal Using Stack</strong></a></li><li><a href="https://www.helpmestudybro.com/preorder-traversal-of-binary-tree-using-stack/"><strong>Preorder Tree Trasversal Using Stack</strong></a></li><li><a href="https://www.helpmestudybro.com/postorder-traversal-of-binary-tree-using-stacks/"><strong>Postorder Tree Traversal Using Stack</strong></a></li><li><a href="https://www.helpmestudybro.com/vertical-traversal-of-binary-tree/"><strong>Vertical Order Tree Traversal</strong></a></li></ol>



<hr class="wp-block-separator has-css-opacity is-style-wide"/>



<p></p>
<p>The post <a href="https://www.helpmestudybro.com/top-view-of-binary-tree/">Top View of Binary Tree</a> appeared first on <a href="https://www.helpmestudybro.com">Helpmestudybro</a>.</p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>Bottom View of Binary Tree</title>
		<link>https://www.helpmestudybro.com/bottom-view-of-binary-tree/</link>
		
		<dc:creator><![CDATA[admin]]></dc:creator>
		<pubDate>Fri, 22 Jan 2021 20:07:22 +0000</pubDate>
				<category><![CDATA[Binary Tree]]></category>
		<category><![CDATA[data structure]]></category>
		<guid isPermaLink="false">https://www.helpmestudybro.com/?p=1226</guid>

					<description><![CDATA[<p>“Bottom View of Binary Tree” is one of the foremost algorithmic problem asked in technical interviews of product-based companies. Here, we are given a binary tree and our task is to write a program to print the bottom view of given binary tree. Bottom View of Binary Tree is referred&#46;&#46;&#46;</p>
<p>The post <a href="https://www.helpmestudybro.com/bottom-view-of-binary-tree/">Bottom View of Binary Tree</a> appeared first on <a href="https://www.helpmestudybro.com">Helpmestudybro</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>“<strong><em>Bottom View of Binary Tree</em></strong>” is one of the foremost algorithmic problem asked in technical interviews of product-based companies. Here, we are given a binary tree and our task is to write a program to print the bottom view of given binary tree.</p>



<p>Bottom View of Binary Tree is referred to those nodes of binary tree which can be seen from bottom of the tree.</p>



<p>We need to print the node values from left to right.</p>



<p>If there are multiple bottom-most nodes for a same horizontal distance from root node, then we need to print the one which occurs later in our traversal algorithm.</p>



<div class="wp-block-image"><figure class="aligncenter size-large"><img loading="lazy" decoding="async" width="401" height="401" src="https://www.helpmestudybro.com/wp-content/uploads/2021/01/BottomView.png" alt="Bottom View of Binary Tree" class="wp-image-1227" srcset="https://www.helpmestudybro.com/wp-content/uploads/2021/01/BottomView.png 401w, https://www.helpmestudybro.com/wp-content/uploads/2021/01/BottomView-300x300.png 300w, https://www.helpmestudybro.com/wp-content/uploads/2021/01/BottomView-150x150.png 150w, https://www.helpmestudybro.com/wp-content/uploads/2021/01/BottomView-80x80.png 80w, https://www.helpmestudybro.com/wp-content/uploads/2021/01/BottomView-320x320.png 320w, https://www.helpmestudybro.com/wp-content/uploads/2021/01/BottomView-50x50.png 50w" sizes="(max-width: 401px) 100vw, 401px" /><figcaption>Bottom View of Binary Tree</figcaption></figure></div>



<p>The idea here is similar to vertical order traversal of binary tree. We will perform the vertical order traversal and print only those nodes which occurs in last of vertical order line.</p>



<p>The steps required to print bottom view of binary tree are as follows:</p>



<ol type="1"><li>Create a map which store the vertical order line number and corresponding bottom node value.</li><li>Create a queue which can accommodate both node and corresponding horizontal distance with root node.</li><li>Insert the root node into queue and 0 as horizontal distance.</li><li>Perform following steps until queue is node empty.<ul><li>Pop front Element of queue.</li><li>Assign the popped node value to map, according to horizontal distance. Every time we encounter a node with already existing horizontal distance node value present in the map, we will replace it.</li><li>If left child exists of popped node, insert into queue with horizontal distance as current HD-1.</li><li>If right child exists of popped node, insert into queue with horizontal distance as current HD+1.</li></ul></li><li>Print the map data which contains bottom view.</li></ol>



<h4 class="wp-block-heading">C++ Program to print bottom view of binary tree are as follows:</h4>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: cpp; title: ; notranslate">
/* C++ Program to print Bottom View of Binary Tree */
#include&lt;bits/stdc++.h&gt;
using namespace std;

typedef struct Node
{
    int data;
    struct Node *left;
    struct Node *right;
    Node(int ele)
    {
        data = ele;
        left = NULL;
        right = NULL;
    }
} Node;

/* Function to print bottom view of Binary Tree */
void bottomView(Node *root)
{
    /* Base Case */
    if(root == NULL)
    return;
    
    /*
        Create a map which store bottom view nodes
        key: vertical order line
        Value: Top node of vertical order line
    */
    map&lt;int, int&gt; mp;
    
    /*
        Create queue which store both node and corresponding HD
    */
    queue&lt;pair&lt;Node*, int&gt;&gt; que;
    int hd = 0;
    
    que.push(make_pair(root,hd));
    
    while(!que.empty())
    {
        /* Pop front Node */
        pair&lt;Node *,int&gt; temp = que.front();
        que.pop();
        Node *temp_node = temp.first;
        int currHD = temp.second;
        
        /* 
            Assign the popped node value to map having same HD.
            Every time we find a node having same horizontal distance,
            we replace its value in the map
        */
        mp&#x5B;currHD] = temp_node -&gt; data;
        
        if(temp_node -&gt; left)
        que.push(make_pair(temp_node -&gt; left, currHD - 1));
        
        if(temp_node -&gt; right)
        que.push(make_pair(temp_node -&gt; right, currHD + 1));
    }
    
    /* Print map, which contain bottom view nodes */
    map&lt;int, int&gt; :: iterator itr;
    for(itr = mp.begin(); itr != mp.end(); ++itr)
    {
        cout &lt;&lt; itr -&gt; second &lt;&lt; &quot; &quot;;
    }
}

int main()
{
    /* Creating a Binary tree and inserting some nodes in it */
    Node *root = NULL;
    
    root = new Node(1);
    root -&gt; left = new Node(2);
    root -&gt; right = new Node(3);
    root -&gt; left -&gt; left = new Node(4);
    root -&gt; left -&gt; right = new Node(5);
    root -&gt; right -&gt; left = new Node(6);
    root -&gt; right -&gt; right = new Node (7);
    root -&gt; left -&gt; right -&gt; left = new Node(8);
    root -&gt; right -&gt; left -&gt; right = new Node(9);
    
    /* Calling function to print bottom view of binary tree*/
    cout&lt;&lt;&quot;The bottom View of Given Binary Tree is:\n&quot;;
    bottomView(root);
}
</pre></div>


<p><strong>OUTPUT:</strong></p>



<pre class="wp-block-preformatted">The bottom View of Given Binary Tree is:
4 8 6 9 7</pre>



<hr class="wp-block-separator is-style-wide"/>



<p><strong>Related Posts:</strong></p>



<ol type="1"><li><a href="https://www.helpmestudybro.com/top-view-of-binary-tree/"><strong>Top View of Binary Tree</strong></a></li><li><a href="https://www.helpmestudybro.com/introduction-to-tree-data-structure/"><strong>Introduction to Tree Data Structure</strong></a></li><li><a href="https://www.helpmestudybro.com/binary-tree-traversal/"><strong>Binary Tree Traversals</strong></a></li><li><a href="https://www.helpmestudybro.com/print-all-leaf-nodes-of-a-binary-tree/"><strong>Print All Leaf Nodes of a Binary Tree</strong></a></li><li><a href="https://www.helpmestudybro.com/count-number-of-nodes-in-a-binary-tree/"><strong>Count Number of Nodes in a Binary Tree</strong></a></li><li><a href="https://www.helpmestudybro.com/print-alternate-levels-of-a-binary-tree/"><strong>Print Alternate Levels of Binary Tree</strong></a></li><li><a href="https://www.helpmestudybro.com/maximum-width-of-binary-tree/"><strong>Maximum Width of Binary Tree</strong></a></li><li><a href="https://www.helpmestudybro.com/level-order-tree-traversal/"><strong>Level Order Tree Traversal</strong></a></li><li><a href="https://www.helpmestudybro.com/left-view-of-binary-tree/"><strong>Left View of Binary Tree</strong></a></li><li><a href="https://www.helpmestudybro.com/right-view-of-binary-tree/"><strong>Right View of Binary Tree</strong></a></li><li><a href="https://www.helpmestudybro.com/compute-height-of-a-binary-tree/"><strong>Compute Height of Binary Tree</strong></a></li><li><a href="https://www.helpmestudybro.com/inorder-tree-traversal-using-stack/"><strong>Inorder Tree Traversal Using Stack</strong></a></li><li><a href="https://www.helpmestudybro.com/preorder-traversal-of-binary-tree-using-stack/"><strong>Preorder Tree Trasversal Using Stack</strong></a></li><li><a href="https://www.helpmestudybro.com/postorder-traversal-of-binary-tree-using-stacks/"><strong>Postorder Tree Traversal Using Stack</strong></a></li><li><a href="https://www.helpmestudybro.com/vertical-traversal-of-binary-tree/"><strong>Vertical Order Tree Traversal</strong></a></li><li><a href="https://www.helpmestudybro.com/delete-binary-tree/"><strong>Delete Complete Binary Tree</strong></a></li><li><a href="https://www.helpmestudybro.com/check-if-two-trees-are-mirror-tree-of-each-other/"><strong>Check if two trees are mirror Trees of Each Other</strong></a></li><li><a href="https://www.helpmestudybro.com/convert-binary-tree-to-its-mirror-tree/"><strong>Convert Binary Tree to its Mirror Tree</strong></a></li><li><a href="https://www.helpmestudybro.com/check-if-binary-tree-is-symmetric-or-not/"><strong>Check if Binary Tree is Symmetric or Not</strong></a></li><li><a href="https://www.helpmestudybro.com/print-all-root-to-leaf-paths-in-a-binary-tree/"><strong>Print All Root to Leaf Paths in a Binary Tree</strong></a></li></ol>



<hr class="wp-block-separator is-style-wide"/>



<p></p>
<p>The post <a href="https://www.helpmestudybro.com/bottom-view-of-binary-tree/">Bottom View of Binary Tree</a> appeared first on <a href="https://www.helpmestudybro.com">Helpmestudybro</a>.</p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>Vertical Traversal of Binary Tree</title>
		<link>https://www.helpmestudybro.com/vertical-traversal-of-binary-tree/</link>
		
		<dc:creator><![CDATA[admin]]></dc:creator>
		<pubDate>Fri, 22 Jan 2021 19:53:35 +0000</pubDate>
				<category><![CDATA[Binary Tree]]></category>
		<category><![CDATA[data structure]]></category>
		<guid isPermaLink="false">https://www.helpmestudybro.com/?p=1222</guid>

					<description><![CDATA[<p>“Vertical Traversal of Binary Tree”&#160;is one of the foremost algorithmic problem based on tree data structure asked in technical interview. Here, we are given a binary tree and our task is to traverse the given binary tree vertically. For vertical order traversal, we need to calculate Horizontal Distance (HD) for&#46;&#46;&#46;</p>
<p>The post <a href="https://www.helpmestudybro.com/vertical-traversal-of-binary-tree/">Vertical Traversal of Binary Tree</a> appeared first on <a href="https://www.helpmestudybro.com">Helpmestudybro</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p><strong>“Vertical Traversal of Binary Tree”&nbsp;</strong>is one of the foremost algorithmic problem based on tree data structure asked in technical interview. Here, we are given a binary tree and our task is to traverse the given binary tree vertically.</p>



<div class="wp-block-image"><figure class="aligncenter size-large"><img loading="lazy" decoding="async" width="401" height="371" src="https://www.helpmestudybro.com/wp-content/uploads/2021/01/VerticalOrderTraversal.png" alt="Vertical Traversal of Binary Tree" class="wp-image-1223" srcset="https://www.helpmestudybro.com/wp-content/uploads/2021/01/VerticalOrderTraversal.png 401w, https://www.helpmestudybro.com/wp-content/uploads/2021/01/VerticalOrderTraversal-300x278.png 300w" sizes="(max-width: 401px) 100vw, 401px" /><figcaption>Vertical Traversal of Binary Tree</figcaption></figure></div>



<p>For vertical order traversal, we need to calculate Horizontal Distance (HD) for each node of given binary tree. Horizontal Distance in case of Binary Tree is defined as distance of node from root node. Horizontal Distance of Root Node is always considered as 0, then right child node will have HD as +1 and left child node will have HD as -1. If two nodes have same horizontal distance, then they are considered to be on same vertical Line.</p>



<p>There can be varied ways to traverse the binary tree vertically. Here, in this post we are going to discuss two methods to implement the Vertical Binary Tree Traversal.</p>



<ol type="1"><li>Preorder Traversal with Hashing</li><li>Level Order Traversal with Hashing</li></ol>



<p><strong>METHOD 1: Preorder Traversal with Hashing</strong></p>



<p>Here, we can recursively calculate Horizontal distance of each node and insert the node into HashMap accordingly.</p>



<p>The steps required to implement Vertical Traversal of Binary Tree are as follows:</p>



<ol type="1"><li>Create a HashMap to store Vertical Order and node value.</li><li>For root, initialize HD = 0.</li><li>Recursively perform following steps:<ul><li>Check and return from Base Case.</li><li>Store the current node into map according to its HD.</li><li>Recursively Call the function for left subtree by passing current HD – 1.</li><li>Recursively Call the function for right subtree by passing current HD + 1.</li></ul></li><li>Print the Vertical traversal stored in HashMap.</li></ol>



<h4 class="wp-block-heading">C++ Program to implement Vertical Traversal of Binary Tree is as follows:</h4>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: cpp; title: ; notranslate">
/* C++ Program to implement vertical traversal of Binary Tree */
#include&lt;bits/stdc++.h&gt;
using namespace std;

typedef struct Node
{
    int data;
    struct Node *left;
    struct Node *right;
    Node(int ele)
    {
        data = ele;
        left = NULL;
        right = NULL;
    }
} Node;

/* Function to insert node values in hashmap according to horizontal distances */
void verticalTraversalRecurr(Node *root, int HD, map&lt;int, vector&lt;int&gt;&gt; &amp;mp)
{
    /* Base Case */
    if(root == NULL)
    return;
    
    /* Store Current Node into Map */
    mp&#x5B;HD].push_back(root -&gt; data);
    
    /* Recursively Call for left subtree by updating HD-1 */
    verticalTraversalRecurr(root -&gt; left, HD-1, mp);
    
    /* Recursively Call for right subtree by updating HD+1 */
    verticalTraversalRecurr(root -&gt; right, HD+1, mp);
}

/* Function to Implement Vertical Traversal of Binary Tree */
void verticalTraversal(Node *root)
{
    /* Base Case */
    if(root == NULL)
    return;
    
    map&lt;int, vector&lt;int&gt;&gt; mp;
    int hd = 0;
    verticalTraversalRecurr(root, hd, mp);
    
    /* Print the Vertical Traversal, stored in HashMap */
    map &lt;int, vector&lt;int&gt;&gt; :: iterator itr;
    for(itr = mp.begin(); itr != mp.end(); ++itr)
    {
        for(int i = 0; i &lt; itr -&gt; second.size(); i++)
        cout &lt;&lt; itr-&gt;second&#x5B;i] &lt;&lt; &quot; &quot;;
        
        cout&lt;&lt;endl;
    }
    
}

int main()
{
    /* Creating a Binary tree and inserting some nodes in it */
    Node *root = NULL;
    
    root = new Node(1);
    root -&gt; left = new Node(2);
    root -&gt; right = new Node(3);
    root -&gt; left -&gt; left = new Node(4);
    root -&gt; left -&gt; right = new Node(5);
    root -&gt; right -&gt; left = new Node(6);
    root -&gt; right -&gt; right = new Node (7);
    root -&gt; left -&gt; right -&gt; left = new Node(8);
    root -&gt; right -&gt; left -&gt; right = new Node(9);
    
    /* Calling function to perform vertical traversal*/
    cout&lt;&lt;&quot;The Veritcal Traversal of the Given Binary Tree is:\n&quot;;
    verticalTraversal(root);
}
</pre></div>


<p><strong>OUTPUT:</strong></p>



<pre class="wp-block-preformatted">The Veritcal Traversal of the Given Binary Tree is:
4&nbsp;
2 8&nbsp;
1 5 6&nbsp;
3 9&nbsp;
7</pre>



<p><strong>METHOD 2: Level Order Traversal using Hashing</strong></p>



<p>Here, we are going modify the queue used in Level order traversal to store both node and corresponding horizontal distance of each node. Also, we need to create a map which store the vertical order line of tree.</p>



<p>The steps required to implement Vertical Traversal of Binary Tree are as follows:</p>



<ol type="1"><li>Create a map which store the vertical order and corresponding nodes.</li><li>Create a queue which can store node and corresponding HD and push root node with corresponding distance as 0.</li><li>Perform following steps until queue is not empty:<ul><li>Pop the element from queue and store it in the map according to vertical order.</li><li>If the popped node has left child, push it into queue and pass HD as current&nbsp;(HD-1).</li><li>If popped node has right child push it into queue and pass HD as current (HD+1).</li></ul></li><li>Display the data of map which stores the vertical order traversal of given binary tree.</li></ol>



<h4 class="wp-block-heading">C++ Program to implement Vertical Traversal of Binary Tree is as follows:</h4>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: cpp; title: ; notranslate">
/* C++ Program to implement vertical traversal of Binary Tree */
#include&lt;bits/stdc++.h&gt;
using namespace std;

typedef struct Node
{
    int data;
    struct Node *left;
    struct Node *right;
    Node(int ele)
    {
        data = ele;
        left = NULL;
        right = NULL;
    }
} Node;

/* Function to Implement Vertical Traversal of Binary Tree */
void verticalTraversal(Node *root)
{
    /* Base Case */
    if(root == NULL)
    return;
    
    /* 
        Create a map which store the vertical order traversal,
        &quot;int&quot; refers to store the vertical line number
        &quot;vector&lt;int&gt;&quot; refers to the chained node values lying on verical line
    */
    map &lt;int, vector&lt;int&gt;&gt; mp;
    int hd = 0;
    
    /*
        Create Queue and every element of queue will store 
        node and corresponding Horizontal Distance
    */
    queue&lt;pair&lt;Node*, int&gt;&gt; que;
    que.push(make_pair(root,hd));
    
    while(!que.empty())
    {
        /* Pop Front Element from queue */
        pair&lt;Node*, int&gt; temp = que.front();
        que.pop();
        
        hd = temp.second;
        Node *temp_node = temp.first;
        
        /* Push the Node into map according to vertical order line */
        mp&#x5B;hd].push_back(temp_node -&gt; data);
        
        if(temp_node -&gt; left)
        que.push(make_pair(temp_node -&gt; left, hd - 1));
        
         if(temp_node -&gt; right)
        que.push(make_pair(temp_node -&gt; right, hd + 1));
    }
    
    /* Print map ehich holds vertical order traversal */
    map&lt;int, vector&lt;int&gt;&gt; :: iterator itr;
    for(itr = mp.begin(); itr != mp.end(); ++itr)
    {
        for(int i = 0; i &lt; itr-&gt;second.size(); i++)
        {
            cout &lt;&lt; itr -&gt; second&#x5B;i] &lt;&lt; &quot; &quot;;
        }
        cout&lt;&lt;endl;
    }
}

int main()
{
    /* Creating a Binary tree and inserting some nodes in it */
    Node *root = NULL;
    
    root = new Node(1);
    root -&gt; left = new Node(2);
    root -&gt; right = new Node(3);
    root -&gt; left -&gt; left = new Node(4);
    root -&gt; left -&gt; right = new Node(5);
    root -&gt; right -&gt; left = new Node(6);
    root -&gt; right -&gt; right = new Node (7);
    root -&gt; left -&gt; right -&gt; left = new Node(8);
    root -&gt; right -&gt; left -&gt; right = new Node(9);
    
    /* Calling function to perform vertical traversal*/
    cout&lt;&lt;&quot;The Veritcal Traversal of the Given Binary Tree is:\n&quot;;
    verticalTraversal(root);
}
</pre></div>


<p><strong>OUTPUT:</strong></p>



<pre class="wp-block-preformatted">The Veritcal Traversal of the Given Binary Tree is:
4&nbsp;
2 8&nbsp;
1 5 6&nbsp;
3 9&nbsp;
7</pre>



<hr class="wp-block-separator is-style-wide"/>



<p><strong>Related Posts:</strong></p>



<ol type="1"><li><a href="https://www.helpmestudybro.com/introduction-to-tree-data-structure/"><strong>Introduction to Tree Data Structure</strong></a></li><li><a href="https://www.helpmestudybro.com/binary-tree-traversal/"><strong>Binary Tree Traversals</strong></a></li><li><a href="https://www.helpmestudybro.com/print-all-leaf-nodes-of-a-binary-tree/"><strong>Print All Leaf Nodes of a Binary Tree</strong></a></li><li><a href="https://www.helpmestudybro.com/count-number-of-nodes-in-a-binary-tree/"><strong>Count Number of Nodes in a Binary Tree</strong></a></li><li><a href="https://www.helpmestudybro.com/print-alternate-levels-of-a-binary-tree/"><strong>Print Alternate Levels of Binary Tree</strong></a></li><li><a href="https://www.helpmestudybro.com/maximum-width-of-binary-tree/"><strong>Maximum Width of Binary Tree</strong></a></li><li><a href="https://www.helpmestudybro.com/level-order-tree-traversal/"><strong>Level Order Tree Traversal</strong></a></li><li><a href="https://www.helpmestudybro.com/left-view-of-binary-tree/"><strong>Left View of Binary Tree</strong></a></li><li><a href="https://www.helpmestudybro.com/right-view-of-binary-tree/"><strong>Right View of Binary Tree</strong></a></li><li><a href="https://www.helpmestudybro.com/compute-height-of-a-binary-tree/"><strong>Compute Height of Binary Tree</strong></a></li><li><a href="https://www.helpmestudybro.com/inorder-tree-traversal-using-stack/"><strong>Inorder Tree Traversal Using Stack</strong></a></li><li><a href="https://www.helpmestudybro.com/preorder-traversal-of-binary-tree-using-stack/"><strong>Preorder Tree Trasversal Using Stack</strong></a></li><li><a href="https://www.helpmestudybro.com/postorder-traversal-of-binary-tree-using-stacks/"><strong>Postorder Tree Traversal Using Stack</strong></a></li><li><a href="https://www.helpmestudybro.com/top-view-of-binary-tree/"><strong>Top View of Binary Tree</strong></a></li><li><a href="https://www.helpmestudybro.com/bottom-view-of-binary-tree/"><strong>Bottom View of Binary Tree</strong></a></li><li><a href="https://www.helpmestudybro.com/delete-binary-tree/"><strong>Delete Complete Binary Tree</strong></a></li><li><a href="https://www.helpmestudybro.com/check-if-two-trees-are-mirror-tree-of-each-other/"><strong>Check if two trees are mirror Trees of Each Other</strong></a></li><li><a href="https://www.helpmestudybro.com/convert-binary-tree-to-its-mirror-tree/"><strong>Convert Binary Tree to its Mirror Tree</strong></a></li><li><a href="https://www.helpmestudybro.com/check-if-binary-tree-is-symmetric-or-not/"><strong>Check if Binary Tree is Symmetric or Not</strong></a></li><li><a href="https://www.helpmestudybro.com/print-all-root-to-leaf-paths-in-a-binary-tree/"><strong>Print All Root to Leaf Paths in a Binary Tree</strong></a></li></ol>



<hr class="wp-block-separator is-style-wide"/>
<p>The post <a href="https://www.helpmestudybro.com/vertical-traversal-of-binary-tree/">Vertical Traversal of Binary Tree</a> appeared first on <a href="https://www.helpmestudybro.com">Helpmestudybro</a>.</p>
]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
