Skip to content

Commit b32f259

Browse files
committed
Reverse order level traversal of a binary tree
1 parent e05bfdd commit b32f259

File tree

3 files changed

+109
-2
lines changed

3 files changed

+109
-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 | 137 |
10-
| Current Streak | 1 days |
9+
| Total Problems | 138 |
10+
| Current Streak | 2 days |
1111
| Longest Streak | 91 ( August 17, 2015 - November 15, 2015 ) |
1212

1313
</center>
@@ -120,6 +120,7 @@ Include contains single header implementation of data structures and some algori
120120
| Given a binary tree and key, node with key exists in tree. Find all the ancestors of the node with key, ancestor here are the nodes which are in straight path from node to root.| [node_ancestors_in_root_path.cpp](tree_problems/node_ancestors_in_root_path.cpp)|
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)|
123+
| 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) |
123124

124125
### String Problems
125126
| Problem | Solution |

sort_search_problems/find_pairs_with_sum.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
1+
/*
2+
* Problem:
3+
* Given an unsorted array of integers, find a pair with given sum in it.
4+
* For example,
5+
* Input:
6+
* arr = [8, 7, 2, 5, 3, 1]
7+
* sum = 10
8+
*
9+
* Output:
10+
* Pair found at index 0 and 2 (8 + 2)
11+
* OR
12+
* Pair found at index 1 and 4 (7 + 3)
13+
*/
14+
15+
116
#include <bits/stdc++.h>
217

318

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/*
2+
* Reverse Level Order Traversal of Binary Tree
3+
* Given a binary tree, print its nodes level by level in reverse order.
4+
* i.e. all nodes present at last level should be printed first followed
5+
* by nodes of second-last level and so on..
6+
* All nodes for any level should be printed from left to right.
7+
*
8+
* 1
9+
* / \
10+
* 2 3
11+
* / \ / \
12+
* 4 5 6 7
13+
*
14+
* The reverse level order traversal of the tree is :
15+
* 4, 5, 6, 7, 2, 3, 1
16+
*/
17+
18+
19+
20+
21+
#include <bits/stdc++.h>
22+
23+
24+
struct TreeNode
25+
{
26+
int data;
27+
TreeNode *left;
28+
TreeNode *right;
29+
TreeNode (int d)
30+
: data {d}
31+
, left {nullptr}
32+
, right {nullptr} {}
33+
};
34+
35+
void printReverseLevelOrder(TreeNode* root)
36+
{
37+
if (!root)
38+
{
39+
return;
40+
}
41+
42+
// Idea is to do a level order traversal and then
43+
// push the nodes in a stack, so we can print in
44+
// reverse order.
45+
//
46+
std::queue<TreeNode*> nodeQueue;
47+
std::stack<TreeNode*> nodeStack;
48+
nodeQueue.push(root);
49+
while (!nodeQueue.empty())
50+
{
51+
TreeNode* curr = nodeQueue.front();
52+
nodeQueue.pop();
53+
nodeStack.push(curr);
54+
55+
// We will push right node first, as we want to print
56+
// reverse level order traversal.
57+
//
58+
if (curr->right)
59+
{
60+
nodeQueue.push(curr->right);
61+
}
62+
63+
if (curr->left)
64+
{
65+
nodeQueue.push(curr->left);
66+
}
67+
}
68+
69+
std::cout << "Reverse level order traversal:";
70+
while (!nodeStack.empty())
71+
{
72+
TreeNode* curr = nodeStack.top();
73+
std::cout << curr->data << " ";
74+
nodeStack.pop();
75+
}
76+
std::cout << std::endl;
77+
}
78+
79+
80+
int main()
81+
{
82+
TreeNode* root = new TreeNode(1);
83+
root->left = new TreeNode(2);
84+
root->right = new TreeNode(3);
85+
root->left->left = new TreeNode(4);
86+
root->left->right = new TreeNode(5);
87+
root->right->left = new TreeNode(6);
88+
root->right->right = new TreeNode(7);
89+
printReverseLevelOrder(root);
90+
return 0;
91+
}

0 commit comments

Comments
 (0)