Skip to content

Commit

Permalink
Program to invert a tree
Browse files Browse the repository at this point in the history
  • Loading branch information
mandliya committed Apr 25, 2017
1 parent b32f259 commit acfab0c
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 2 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

| Current Status| Stats |
| :------------: | :----------: |
| Total Problems | 138 |
| Current Streak | 2 days |
| Total Problems | 139 |
| Current Streak | 3 days |
| Longest Streak | 91 ( August 17, 2015 - November 15, 2015 ) |

</center>
Expand Down Expand Up @@ -121,6 +121,7 @@ Include contains single header implementation of data structures and some algori
| 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)|
| 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)|
| 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) |
| Invert a binary tree, recursively and iteratively.| [invert_a_tree.cpp](tree_problems/invert_a_tree.cpp) |

### String Problems
| Problem | Solution |
Expand Down
117 changes: 117 additions & 0 deletions tree_problems/invert_a_tree.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
/*
* Invert given Binary Tree | Recursive and Iterative solution
* 1 1
* / \ / \
* 2 3 -> 3 2
* / \ / \ / \ / \
* 4 5 6 7 7 6 5 4
*
*/

#include <bits/stdc++.h>

struct TreeNode
{
int data;
TreeNode *left;
TreeNode *right;
TreeNode(int d)
: data{d}, left{nullptr}, right{nullptr} {}
};

void invertTreeRecur(TreeNode* root)
{
if (!root)
{
return;
}

// Swap left and right trees
//
TreeNode* temp = root->left;
root->left = root->right;
root->right = temp;

// Do this recursively for left and right sub-trees.
//
invertTreeRecur(root->left);
invertTreeRecur(root->right);
}

void invertTreeIter(TreeNode* root)
{
if (!root)
{
return;
}
std::queue<TreeNode*> nodeQueue;
nodeQueue.push(root);
while(!nodeQueue.empty())
{
TreeNode* curr = nodeQueue.front();
nodeQueue.pop();

// Swap left and right children
//
TreeNode* temp = curr->left;
curr->left = curr->right;
curr->right = temp;

if (curr->left)
{
nodeQueue.push(curr->left);
}

if (curr->right)
{
nodeQueue.push(curr->right);
}
}
}

// Prints post order traversal of tree, where level expands left to right.
//
void postOrder(TreeNode* root, int indent = 0)
{
if (root)
{
if(root->right)
{
postOrder(root->right, indent+4);
}
if (indent) {
std::cout << std::setw(indent) << ' ';
}
if (root->right)
{
std::cout<<" /\n" << std::setw(indent) << ' ';
}
std::cout<< root->data << "\n ";
if(root->left)
{
std::cout << std::setw(indent) << ' ' <<" \\\n";
postOrder(root->left, indent + 4);
}
}
}

int main()
{
TreeNode *root = new TreeNode(1);
root->left = new TreeNode(2);
root->right = new TreeNode(3);
root->left->left = new TreeNode(4);
root->left->right = new TreeNode(5);
root->right->left = new TreeNode(6);
root->right->right = new TreeNode(7);
std::cout << "Current Tree: \n";
postOrder(root);
std::cout << "\nInverting it recursively:\n";
invertTreeRecur(root);
postOrder(root);
std::cout << "\nInverting it again iteratively:\n";
invertTreeIter(root);
postOrder(root);
std::cout << std::endl;
return 0;
}

0 comments on commit acfab0c

Please sign in to comment.