File tree Expand file tree Collapse file tree 2 files changed +93
-0
lines changed
lcof2/剑指 Offer II 046. 二叉树的右侧视图 Expand file tree Collapse file tree 2 files changed +93
-0
lines changed Original file line number Diff line number Diff line change 67
67
68
68
```
69
69
70
+ ### ** C++**
71
+
72
+ ``` cpp
73
+ class Solution
74
+ {
75
+ public:
76
+ vector<int > rightSideView ( TreeNode* root )
77
+ {
78
+ vector<int > res;
79
+
80
+ if ( !root )
81
+ {
82
+ return res;
83
+ }
84
+
85
+ queue<TreeNode* > que;
86
+ que.push ( root );
87
+
88
+ while ( !que.empty() )
89
+ {
90
+ int size = que.size();
91
+
92
+ for ( int i = 0; i < size; i++ )
93
+ {
94
+ TreeNode* ptr = que.front();
95
+ que.pop();
96
+
97
+ if ( i == size - 1 )
98
+ {
99
+ res.push_back ( ptr->val );
100
+ }
101
+
102
+ if ( ptr->left )
103
+ {
104
+ que.push ( ptr->left );
105
+ }
106
+
107
+ if ( ptr-> right )
108
+ {
109
+ que.push ( ptr->right );
110
+ }
111
+ }
112
+ }
113
+
114
+ return res;
115
+ }
116
+ };
117
+ ```
118
+
70
119
### ** ...**
71
120
72
121
```
Original file line number Diff line number Diff line change
1
+ class Solution
2
+ {
3
+ public:
4
+ vector<int > rightSideView ( TreeNode* root )
5
+ {
6
+ vector<int > res;
7
+
8
+ if ( !root )
9
+ {
10
+ return res;
11
+ }
12
+
13
+ queue<TreeNode* > que;
14
+ que.push ( root );
15
+
16
+ while ( !que.empty () )
17
+ {
18
+ int size = que.size ();
19
+
20
+ for ( int i = 0 ; i < size; i++ )
21
+ {
22
+ TreeNode* ptr = que.front ();
23
+ que.pop ();
24
+
25
+ if ( i == size - 1 )
26
+ {
27
+ res.push_back ( ptr->val );
28
+ }
29
+
30
+ if ( ptr->left )
31
+ {
32
+ que.push ( ptr->left );
33
+ }
34
+
35
+ if ( ptr-> right )
36
+ {
37
+ que.push ( ptr->right );
38
+ }
39
+ }
40
+ }
41
+
42
+ return res;
43
+ }
44
+ };
You can’t perform that action at this time.
0 commit comments