Skip to content

Commit 7c72595

Browse files
committed
feat: add solutions to lc problem: No.0199
* Add solutions to lc problem: No.0199. Binary Tree Pruning * Add solutions to lcof2 problem: No.048 * Update solutions to lcof2 problems: No.046,047
1 parent 2a96ec9 commit 7c72595

File tree

21 files changed

+944
-192
lines changed

21 files changed

+944
-192
lines changed

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

+53-72
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949

5050
<!-- 这里可写通用的实现逻辑 -->
5151

52-
层序遍历,取每层最后一个元素
52+
层序遍历,取每层最后一个元素
5353

5454
<!-- tabs:start -->
5555

@@ -72,14 +72,13 @@ class Solution:
7272
d = deque([root])
7373
while d:
7474
n = len(d)
75+
ans.append(d[0].val)
7576
for i in range(n):
7677
node = d.popleft()
77-
if i == n - 1:
78-
ans.append(node.val)
79-
if node.left:
80-
d.append(node.left)
8178
if node.right:
8279
d.append(node.right)
80+
if node.left:
81+
d.append(node.left)
8382
return ans
8483
```
8584

@@ -109,21 +108,18 @@ class Solution {
109108
if (root == null) {
110109
return ans;
111110
}
112-
Queue<TreeNode> q = new ArrayDeque<>();
111+
Deque<TreeNode> q = new ArrayDeque<>();
113112
q.offer(root);
114113
while (!q.isEmpty()) {
115-
int n = q.size();
116-
for (int i = 0; i < n; i++) {
114+
ans.add(q.peekFirst().val);
115+
for (int i = q.size(); i > 0; --i) {
117116
TreeNode node = q.poll();
118-
if (i == n - 1) {
119-
ans.add(node.val);
117+
if (node.right != null) {
118+
q.offer(node.right);
120119
}
121120
if (node.left != null) {
122121
q.offer(node.left);
123122
}
124-
if (node.right != null) {
125-
q.offer(node.right);
126-
}
127123
}
128124
}
129125
return ans;
@@ -134,48 +130,35 @@ class Solution {
134130
### **C++**
135131

136132
```cpp
137-
class Solution
138-
{
133+
/**
134+
* Definition for a binary tree node.
135+
* struct TreeNode {
136+
* int val;
137+
* TreeNode *left;
138+
* TreeNode *right;
139+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
140+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
141+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
142+
* };
143+
*/
144+
class Solution {
139145
public:
140-
vector<int> rightSideView ( TreeNode* root )
141-
{
142-
vector<int> res;
143-
144-
if ( !root )
146+
vector<int> rightSideView(TreeNode* root) {
147+
vector<int> ans;
148+
if (!root) return ans;
149+
queue<TreeNode*> q;
150+
q.push(root);
151+
while (!q.empty())
145152
{
146-
return res;
147-
}
148-
149-
queue<TreeNode* > que;
150-
que.push ( root );
151-
152-
while ( !que.empty() )
153-
{
154-
int size = que.size();
155-
156-
for ( int i = 0; i < size; i++ )
157-
{
158-
TreeNode* ptr = que.front();
159-
que.pop();
160-
161-
if ( i == size - 1 )
162-
{
163-
res.push_back ( ptr->val );
164-
}
165-
166-
if ( ptr->left )
167-
{
168-
que.push ( ptr->left );
169-
}
170-
171-
if ( ptr-> right )
172-
{
173-
que.push ( ptr->right );
174-
}
153+
ans.push_back(q.front()->val);
154+
for (int i = q.size(); i > 0; --i) {
155+
auto node = q.front();
156+
q.pop();
157+
if (node->right) q.push(node->right);
158+
if (node->left) q.push(node->left);
175159
}
176160
}
177-
178-
return res;
161+
return ans;
179162
}
180163
};
181164
```
@@ -192,27 +175,25 @@ public:
192175
* }
193176
*/
194177
func rightSideView(root *TreeNode) []int {
195-
var ans []int
196-
if root == nil {
197-
return ans
198-
}
199-
q := []*TreeNode{root}
200-
for n := len(q); n > 0; n = len(q) {
201-
for i := 0; i < n; i++ {
202-
node := q[0]
203-
q = q[1:]
204-
if i == n - 1 {
205-
ans = append(ans, node.Val)
206-
}
207-
if node.Left != nil {
208-
q = append(q, node.Left)
209-
}
210-
if node.Right != nil {
211-
q = append(q, node.Right)
212-
}
213-
}
214-
}
215-
return ans
178+
var ans []int
179+
if root == nil {
180+
return ans
181+
}
182+
q := []*TreeNode{root}
183+
for len(q) > 0 {
184+
ans = append(ans, q[0].Val)
185+
for i := len(q); i > 0; i-- {
186+
node := q[0]
187+
q = q[1:]
188+
if node.Right != nil {
189+
q = append(q, node.Right)
190+
}
191+
if node.Left != nil {
192+
q = append(q, node.Left)
193+
}
194+
}
195+
}
196+
return ans
216197
}
217198
```
218199

Original file line numberDiff line numberDiff line change
@@ -1,44 +1,31 @@
1-
class Solution
2-
{
1+
/**
2+
* Definition for a binary tree node.
3+
* struct TreeNode {
4+
* int val;
5+
* TreeNode *left;
6+
* TreeNode *right;
7+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
8+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
9+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
10+
* };
11+
*/
12+
class Solution {
313
public:
4-
vector<int> rightSideView ( TreeNode* root )
5-
{
6-
vector<int> res;
7-
8-
if ( !root )
14+
vector<int> rightSideView(TreeNode* root) {
15+
vector<int> ans;
16+
if (!root) return ans;
17+
queue<TreeNode*> q;
18+
q.push(root);
19+
while (!q.empty())
920
{
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-
}
21+
ans.push_back(q.front()->val);
22+
for (int i = q.size(); i > 0; --i) {
23+
auto node = q.front();
24+
q.pop();
25+
if (node->right) q.push(node->right);
26+
if (node->left) q.push(node->left);
3927
}
4028
}
41-
42-
return res;
29+
return ans;
4330
}
44-
};
31+
};

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

+5-7
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,17 @@ func rightSideView(root *TreeNode) []int {
1212
return ans
1313
}
1414
q := []*TreeNode{root}
15-
for n := len(q); n > 0; n = len(q) {
16-
for i := 0; i < n; i++ {
15+
for len(q) > 0 {
16+
ans = append(ans, q[0].Val)
17+
for i := len(q); i > 0; i-- {
1718
node := q[0]
1819
q = q[1:]
19-
if i == n-1 {
20-
ans = append(ans, node.Val)
20+
if node.Right != nil {
21+
q = append(q, node.Right)
2122
}
2223
if node.Left != nil {
2324
q = append(q, node.Left)
2425
}
25-
if node.Right != nil {
26-
q = append(q, node.Right)
27-
}
2826
}
2927
}
3028
return ans

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

+6-9
Original file line numberDiff line numberDiff line change
@@ -19,23 +19,20 @@ public List<Integer> rightSideView(TreeNode root) {
1919
if (root == null) {
2020
return ans;
2121
}
22-
Queue<TreeNode> q = new ArrayDeque<>();
22+
Deque<TreeNode> q = new ArrayDeque<>();
2323
q.offer(root);
2424
while (!q.isEmpty()) {
25-
int n = q.size();
26-
for (int i = 0; i < n; i++) {
25+
ans.add(q.peekFirst().val);
26+
for (int i = q.size(); i > 0; --i) {
2727
TreeNode node = q.poll();
28-
if (i == n - 1) {
29-
ans.add(node.val);
28+
if (node.right != null) {
29+
q.offer(node.right);
3030
}
3131
if (node.left != null) {
3232
q.offer(node.left);
3333
}
34-
if (node.right != null) {
35-
q.offer(node.right);
36-
}
3734
}
3835
}
3936
return ans;
4037
}
41-
}
38+
}

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

+3-4
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,11 @@ def rightSideView(self, root: TreeNode) -> List[int]:
1212
d = deque([root])
1313
while d:
1414
n = len(d)
15+
ans.append(d[0].val)
1516
for i in range(n):
1617
node = d.popleft()
17-
if i == n - 1:
18-
ans.append(node.val)
19-
if node.left:
20-
d.append(node.left)
2118
if node.right:
2219
d.append(node.right)
20+
if node.left:
21+
d.append(node.left)
2322
return ans

lcof2/剑指 Offer II 047. 二叉树剪枝/README.md

+26
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,32 @@ func pruneTree(root *TreeNode) *TreeNode {
143143
}
144144
```
145145

146+
### **C++**
147+
148+
```cpp
149+
/**
150+
* Definition for a binary tree node.
151+
* struct TreeNode {
152+
* int val;
153+
* TreeNode *left;
154+
* TreeNode *right;
155+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
156+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
157+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
158+
* };
159+
*/
160+
class Solution {
161+
public:
162+
TreeNode* pruneTree(TreeNode* root) {
163+
if (!root) return nullptr;
164+
root->left = pruneTree(root->left);
165+
root->right = pruneTree(root->right);
166+
if (!root->val && !root->left && !root->right) return nullptr;
167+
return root;
168+
}
169+
};
170+
```
171+
146172
### **...**
147173
148174
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* struct TreeNode {
4+
* int val;
5+
* TreeNode *left;
6+
* TreeNode *right;
7+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
8+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
9+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
10+
* };
11+
*/
12+
class Solution {
13+
public:
14+
TreeNode* pruneTree(TreeNode* root) {
15+
if (!root) return nullptr;
16+
root->left = pruneTree(root->left);
17+
root->right = pruneTree(root->right);
18+
if (!root->val && !root->left && !root->right) return nullptr;
19+
return root;
20+
}
21+
};

0 commit comments

Comments
 (0)