-
Notifications
You must be signed in to change notification settings - Fork 288
/
AC_two_queue_n.cpp
51 lines (41 loc) · 1.04 KB
/
AC_two_queue_n.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
/*
* Author: illuz <iilluzen[at]gmail.com>
* File: AC_two_queue_n.cpp
* Create Date: 2015-04-05 09:17:30
* Descripton: Use two queue to implement level order.
*/
#include <bits/stdc++.h>
using namespace std;
const int N = 0;
// Definition for binary tree
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
class Solution {
public:
vector<int> rightSideView(TreeNode *root) {
queue<TreeNode *> pre;
vector<int> ret;
if (!root) return ret;
pre.push(root);
while (!pre.empty()) {
ret.push_back(pre.front()->val);
queue<TreeNode *> cur;
while (!pre.empty()) {
if (pre.front()->right)
cur.push(pre.front()->right);
if (pre.front()->left)
cur.push(pre.front()->left);
pre.pop();
}
swap(pre, cur);
}
return ret;
}
};
int main() {
return 0;
}