-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path199.cpp
38 lines (31 loc) · 829 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
// 199. Binary Tree Right Side View - https://leetcode.com/problems/binary-tree-right-side-view
#include "bits/stdc++.h"
using namespace std;
// Definition for a binary tree node.
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
};
class Solution {
private:
vector<int> result;
public:
void preorder(TreeNode* node, int level) {
if (node == nullptr) { return; }
if (result.size() <= level) {
result.emplace_back(node->val);
}
preorder(node->right, level + 1);
preorder(node->left, level + 1);
}
vector<int> rightSideView(TreeNode* root) {
preorder(root, 0);
return result;
}
};
int main() {
ios::sync_with_stdio(false);
return 0;
}