-
Notifications
You must be signed in to change notification settings - Fork 0
/
107-BinaryTreeLevelOrderTraversalII.cpp
53 lines (49 loc) · 1.57 KB
/
107-BinaryTreeLevelOrderTraversalII.cpp
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
/*=============================================================================
# FileName: 107-BinaryTreeLevelOrderTraversalII.cpp
# Desc: AC, 4ms
# Author: Jian Huang
# Email: [email protected]
# HomePage: https://cn.linkedin.com/in/huangjian1993
# Version: 0.0.1
# LastChange: 2015-08-20 09:50:36
# History:
=============================================================================*/
#include <leetcode.h>
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> > result;
vector<int> ret;
queue<TreeNode *> nodeQueue;
if (!root) {
return result;
}
nodeQueue.push(root);
int count = 1;
while (!nodeQueue.empty()) {
TreeNode *node = nodeQueue.front();
nodeQueue.pop();
ret.push_back(node->val);
if (node->left) {
nodeQueue.push(node->left);
}
if (node->right) {
nodeQueue.push(node->right);
}
count --;
if (count == 0) {
count = nodeQueue.size();
result.push_back(ret);
ret.clear();
}
}
reverse(result.begin(), result.end());
return result;
}
};