-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path199.cpp
40 lines (39 loc) · 913 Bytes
/
199.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
//
// 199.cpp
// leetcode
//
// Created by R Z on 2018/8/12.
// Copyright © 2018年 R Z. All rights reserved.
//
#include <stdio.h>
#include <queue>
#include <vector>
using namespace std;
/**
* Definition for a binary tree node.*/
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) {
vector<int> res;
if(root==NULL) return res;
queue<TreeNode*> q;
q.push(root);
while(!q.empty()){
int n=q.size();
for(int i=0;i<n;i++){
TreeNode* tmp=q.front();
if(i==n-1) res.push_back(tmp->val);
q.pop();
if(tmp->left) q.push(tmp->left);
if(tmp->right) q.push(tmp->right);
}
}
return res;
}
};