Skip to content

Commit f41bad2

Browse files
daliang111willrlzhangmaolonglong
authored
feat: add cpp solution to lcof2 problem: NO.046 (doocs#567)
Co-authored-by: willrlzhang <willrlzhang@tencent.com> Co-authored-by: 陈劭珑 <382084620@qq.com>
1 parent dcc11b6 commit f41bad2

File tree

2 files changed

+93
-0
lines changed

2 files changed

+93
-0
lines changed

lcof2/剑指 Offer II 046. 二叉树的右侧视图/README.md

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,55 @@
6767

6868
```
6969

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+
70119
### **...**
71120

72121
```
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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+
};

0 commit comments

Comments
 (0)