-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path107. Binary Tree Level Order Traversal II.cpp
More file actions
47 lines (44 loc) · 1.08 KB
/
Copy path107. Binary Tree Level Order Traversal II.cpp
File metadata and controls
47 lines (44 loc) · 1.08 KB
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
//
// 107. Binary Tree Level Order Traversal II.cpp
// leetcode
//
// Created by R Z on 2017/9/27.
// Copyright © 2017年 R Z. All rights reserved.
//
#include <stdio.h>
#include <vector>
#include <stack>
#include <queue>
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) {
stack<vector<int>> tower;
vector<vector<int>> res;
queue<TreeNode*> q;
if(root) q.push(root);
while(!q.empty()){
vector<int> temp;
int s = q.size();
for(int i=0; i<s; i++){
TreeNode* t = q.front();
q.pop();
if(t->left) q.push(t->left);
if(t->right) q.push(t->right);
temp.push_back(t->val);
}
tower.push(temp);
}
while(!tower.empty()){
res.push_back(tower.top());
tower.pop();
}
return res;
}
};