Skip to content

Commit acfab0c

Browse files
committed
Program to invert a tree
1 parent b32f259 commit acfab0c

File tree

2 files changed

+120
-2
lines changed

2 files changed

+120
-2
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66

77
| Current Status| Stats |
88
| :------------: | :----------: |
9-
| Total Problems | 138 |
10-
| Current Streak | 2 days |
9+
| Total Problems | 139 |
10+
| Current Streak | 3 days |
1111
| Longest Streak | 91 ( August 17, 2015 - November 15, 2015 ) |
1212

1313
</center>
@@ -121,6 +121,7 @@ Include contains single header implementation of data structures and some algori
121121
| Given a binary tree and key, return the level of the node with key. Root is at level 1, and if node with key does not exists in tree, return 0| [level_of_node.cpp](tree_problems/level_of_node.cpp)|
122122
| Given a binary tree, find all the paths from root to nodes, whose sum is k. | [k_sum_paths.cpp](tree_problems/k_sum_paths.cpp)|
123123
| Given a binary tree, print its nodes level by level in reverse order. i.e. all nodes present at last level should be printed first followed by nodes of second-last level and so on.. All nodes for any level should be printed from left to right. | [reverseLevelOrderTraversal.cpp](tree_problems/reverseLevelOrderTraversal.cpp) |
124+
| Invert a binary tree, recursively and iteratively.| [invert_a_tree.cpp](tree_problems/invert_a_tree.cpp) |
124125

125126
### String Problems
126127
| Problem | Solution |

tree_problems/invert_a_tree.cpp

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
/*
2+
* Invert given Binary Tree | Recursive and Iterative solution
3+
* 1 1
4+
* / \ / \
5+
* 2 3 -> 3 2
6+
* / \ / \ / \ / \
7+
* 4 5 6 7 7 6 5 4
8+
*
9+
*/
10+
11+
#include <bits/stdc++.h>
12+
13+
struct TreeNode
14+
{
15+
int data;
16+
TreeNode *left;
17+
TreeNode *right;
18+
TreeNode(int d)
19+
: data{d}, left{nullptr}, right{nullptr} {}
20+
};
21+
22+
void invertTreeRecur(TreeNode* root)
23+
{
24+
if (!root)
25+
{
26+
return;
27+
}
28+
29+
// Swap left and right trees
30+
//
31+
TreeNode* temp = root->left;
32+
root->left = root->right;
33+
root->right = temp;
34+
35+
// Do this recursively for left and right sub-trees.
36+
//
37+
invertTreeRecur(root->left);
38+
invertTreeRecur(root->right);
39+
}
40+
41+
void invertTreeIter(TreeNode* root)
42+
{
43+
if (!root)
44+
{
45+
return;
46+
}
47+
std::queue<TreeNode*> nodeQueue;
48+
nodeQueue.push(root);
49+
while(!nodeQueue.empty())
50+
{
51+
TreeNode* curr = nodeQueue.front();
52+
nodeQueue.pop();
53+
54+
// Swap left and right children
55+
//
56+
TreeNode* temp = curr->left;
57+
curr->left = curr->right;
58+
curr->right = temp;
59+
60+
if (curr->left)
61+
{
62+
nodeQueue.push(curr->left);
63+
}
64+
65+
if (curr->right)
66+
{
67+
nodeQueue.push(curr->right);
68+
}
69+
}
70+
}
71+
72+
// Prints post order traversal of tree, where level expands left to right.
73+
//
74+
void postOrder(TreeNode* root, int indent = 0)
75+
{
76+
if (root)
77+
{
78+
if(root->right)
79+
{
80+
postOrder(root->right, indent+4);
81+
}
82+
if (indent) {
83+
std::cout << std::setw(indent) << ' ';
84+
}
85+
if (root->right)
86+
{
87+
std::cout<<" /\n" << std::setw(indent) << ' ';
88+
}
89+
std::cout<< root->data << "\n ";
90+
if(root->left)
91+
{
92+
std::cout << std::setw(indent) << ' ' <<" \\\n";
93+
postOrder(root->left, indent + 4);
94+
}
95+
}
96+
}
97+
98+
int main()
99+
{
100+
TreeNode *root = new TreeNode(1);
101+
root->left = new TreeNode(2);
102+
root->right = new TreeNode(3);
103+
root->left->left = new TreeNode(4);
104+
root->left->right = new TreeNode(5);
105+
root->right->left = new TreeNode(6);
106+
root->right->right = new TreeNode(7);
107+
std::cout << "Current Tree: \n";
108+
postOrder(root);
109+
std::cout << "\nInverting it recursively:\n";
110+
invertTreeRecur(root);
111+
postOrder(root);
112+
std::cout << "\nInverting it again iteratively:\n";
113+
invertTreeIter(root);
114+
postOrder(root);
115+
std::cout << std::endl;
116+
return 0;
117+
}

0 commit comments

Comments
 (0)