-
Notifications
You must be signed in to change notification settings - Fork 0
/
binarySearchTreeTraversalButtomUp.cc
67 lines (58 loc) · 1.25 KB
/
binarySearchTreeTraversalButtomUp.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/**
* Definition for binary tree
*/
#include <iostream>
#include <stddef.h>
#include <queue>
#include <vector>
#include <algorithm>
using namespace std;
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
class Solution {
public:
vector<vector<int> > levelOrderBottom(TreeNode *root) {
vector<vector<int> > res;
if (!root)
return res;
queue<TreeNode *> que;
que.push(root);
vector<int> tmp;
int count = que.size();
while (!que.empty()) {
TreeNode *a = que.front();
que.pop();
tmp.push_back(a->val);
if (a->left)
que.push(a->left);
if (a->right)
que.push(a->right);
count--;
if (!count) {
count = que.size();
res.push_back(tmp);
tmp.clear();
}
}
reverse(res.begin(), res.end());
return res;
}
};
int main(int argc, char **argv) {
TreeNode tmp(1);
TreeNode tmp1(2);
tmp.right = &tmp1;
Solution sol;
vector<vector<int> > ret = sol.levelOrderBottom(&tmp);
for (int i = 0; i < ret.size(); ++i) {
vector<int> t1 = ret[i];
for (int j = 0; j < t1.size(); ++j)
cout << t1[j] << " ";
cout << endl;
}
return 0;
}