Skip to content

Commit b3ea654

Browse files
committed
feat: update leetcode solutions: No.0199. Binary Tree Right Side View
1 parent 692acff commit b3ea654

File tree

7 files changed

+183
-47
lines changed

7 files changed

+183
-47
lines changed

.github/workflows/contributors.yml

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
name: Contributors
2+
3+
on:
4+
schedule:
5+
- cron: "0/10 * * * *"
6+
push:
7+
branches:
8+
- main
9+
10+
jobs:
11+
contributors:
12+
runs-on: ubuntu-latest
13+
if: github.repository == 'doocs/leetcode'
14+
steps:
15+
- uses: bubkoo/contributors-list@v1
16+
name: contributors-doocs-leetcode
17+
with:
18+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
19+
svgPath: images/contributors.svg
20+
svgWidth: 890
21+
round: true
22+
includeBots: true

README.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@
108108
- [翻转二叉树](/solution/0200-0299/0226.Invert%20Binary%20Tree/README.md)
109109
- [二叉树的层次遍历](/solution/0100-0199/0102.Binary%20Tree%20Level%20Order%20Traversal/README.md)
110110
- [二叉树的层次遍历 II](/solution/0100-0199/0107.Binary%20Tree%20Level%20Order%20Traversal%20II/README.md)
111+
- [二叉树的右视图](/solution/0100-0199/0199.Binary%20Tree%20Right%20Side%20View/README.md)
111112
- [二叉树的最大深度](/solution/0100-0199/0104.Maximum%20Depth%20of%20Binary%20Tree/README.md)
112113
- [二叉树的最小深度](/solution/0100-0199/0111.Minimum%20Depth%20of%20Binary%20Tree/README.md)
113114
- [二叉树的所有路径](/solution/0200-0299/0257.Binary%20Tree%20Paths/README.md)
@@ -190,7 +191,9 @@
190191

191192
非常感谢以下所有朋友对本项目的贡献,你们是最可爱的人!
192193

193-
<a href="https://opencollective.com/doocs-leetcode/contributors.svg?width=890&button=true" target="_blank"><img src="https://opencollective.com/doocs-leetcode/contributors.svg?width=890&button=false" /></a>
194+
<!-- https://opencollective.com/doocs-leetcode/contributors.svg?width=890&button=false -->
195+
196+
<a href="https://github.com/doocs/leetcode/graphs/contributors" target="_blank"><img src="./images/contributors.svg" /></a>
194197

195198
## 赞助者
196199

README_EN.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ Complete solutions to [LeetCode](https://leetcode-cn.com/problemset/all/), [LCOF
105105
- [Invert Binary Tree](/solution/0200-0299/0226.Invert%20Binary%20Tree/README_EN.md)
106106
- [Binary Tree Level Order Traversal](/solution/0100-0199/0102.Binary%20Tree%20Level%20Order%20Traversal/README_EN.md)
107107
- [Binary Tree Level Order Traversal II](/solution/0100-0199/0107.Binary%20Tree%20Level%20Order%20Traversal%20II/README_EN.md)
108+
- [Binary Tree Right Side View](/solution/0100-0199/0199.Binary%20Tree%20Right%20Side%20View/README_EN.md)
108109
- [Maximum Depth of Binary Tree](/solution/0100-0199/0104.Maximum%20Depth%20of%20Binary%20Tree/README_EN.md)
109110
- [Minimum Depth of Binary Tree](/solution/0100-0199/0111.Minimum%20Depth%20of%20Binary%20Tree/README_EN.md)
110111
- [Binary Tree Paths](/solution/0200-0299/0257.Binary%20Tree%20Paths/README_EN.md)
@@ -180,7 +181,9 @@ You can also contribute to [doocs/leetcode](https://github.com/doocs/leetcode) u
180181

181182
This project exists thanks to all the people who contribute.
182183

183-
<a href="https://opencollective.com/doocs-leetcode/contributors.svg?width=890&button=true" target="_blank"><img src="https://opencollective.com/doocs-leetcode/contributors.svg?width=890&button=false" /></a>
184+
<!-- https://opencollective.com/doocs-leetcode/contributors.svg?width=890&button=false -->
185+
186+
<a href="https://github.com/doocs/leetcode/graphs/contributors" target="_blank"><img src="./images/contributors.svg" /></a>
184187

185188
## Backers & Sponsors
186189

solution/0100-0199/0199.Binary Tree Right Side View/README.md

+52-15
Original file line numberDiff line numberDiff line change
@@ -24,38 +24,75 @@
2424

2525
<!-- 这里可写通用的实现逻辑 -->
2626

27+
队列实现。
28+
2729
<!-- tabs:start -->
2830

2931
### **Python3**
3032

3133
<!-- 这里可写当前语言的特殊实现逻辑 -->
3234

3335
```python
34-
36+
# Definition for a binary tree node.
37+
# class TreeNode:
38+
# def __init__(self, val=0, left=None, right=None):
39+
# self.val = val
40+
# self.left = left
41+
# self.right = right
42+
class Solution:
43+
def rightSideView(self, root: TreeNode) -> List[int]:
44+
if not root:
45+
return []
46+
q = [root]
47+
res = []
48+
while q:
49+
size = len(q)
50+
res.append(q[0].val)
51+
for _ in range(size):
52+
node = q.pop(0)
53+
if node.right:
54+
q.append(node.right)
55+
if node.left:
56+
q.append(node.left)
57+
return res
3558
```
3659

3760
### **Java**
3861

3962
<!-- 这里可写当前语言的特殊实现逻辑 -->
4063

4164
```java
65+
/**
66+
* Definition for a binary tree node.
67+
* public class TreeNode {
68+
* int val;
69+
* TreeNode left;
70+
* TreeNode right;
71+
* TreeNode() {}
72+
* TreeNode(int val) { this.val = val; }
73+
* TreeNode(int val, TreeNode left, TreeNode right) {
74+
* this.val = val;
75+
* this.left = left;
76+
* this.right = right;
77+
* }
78+
* }
79+
*/
4280
class Solution {
4381
public List<Integer> rightSideView(TreeNode root) {
44-
List<Integer> ans = new ArrayList<>();
45-
robot(root, ans, 0);
46-
return ans;
47-
}
48-
49-
private void robot(TreeNode root, List<Integer> ans, int level) {
50-
if (root == null) {
51-
return;
52-
}
53-
if (ans.size() <= level) {
54-
ans.add(root.val);
82+
if (root == null) return Collections.emptyList();
83+
Deque<TreeNode> q = new ArrayDeque<>();
84+
q.offer(root);
85+
List<Integer> res = new ArrayList<>();
86+
while (!q.isEmpty()) {
87+
int size = q.size();
88+
res.add(q.peek().val);
89+
while (size-- > 0) {
90+
TreeNode node = q.poll();
91+
if (node.right != null) q.offer(node.right);
92+
if (node.left != null) q.offer(node.left);
93+
}
5594
}
56-
ans.set(level, root.val);
57-
robot(root.left, ans, level + 1);
58-
robot(root.right, ans, level + 1);
95+
return res;
5996
}
6097
}
6198
```

solution/0100-0199/0199.Binary Tree Right Side View/README_EN.md

+50-15
Original file line numberDiff line numberDiff line change
@@ -37,29 +37,64 @@
3737
### **Python3**
3838

3939
```python
40-
40+
# Definition for a binary tree node.
41+
# class TreeNode:
42+
# def __init__(self, val=0, left=None, right=None):
43+
# self.val = val
44+
# self.left = left
45+
# self.right = right
46+
class Solution:
47+
def rightSideView(self, root: TreeNode) -> List[int]:
48+
if not root:
49+
return []
50+
q = [root]
51+
res = []
52+
while q:
53+
size = len(q)
54+
res.append(q[0].val)
55+
for _ in range(size):
56+
node = q.pop(0)
57+
if node.right:
58+
q.append(node.right)
59+
if node.left:
60+
q.append(node.left)
61+
return res
4162
```
4263

4364
### **Java**
4465

4566
```java
67+
/**
68+
* Definition for a binary tree node.
69+
* public class TreeNode {
70+
* int val;
71+
* TreeNode left;
72+
* TreeNode right;
73+
* TreeNode() {}
74+
* TreeNode(int val) { this.val = val; }
75+
* TreeNode(int val, TreeNode left, TreeNode right) {
76+
* this.val = val;
77+
* this.left = left;
78+
* this.right = right;
79+
* }
80+
* }
81+
*/
4682
class Solution {
4783
public List<Integer> rightSideView(TreeNode root) {
48-
List<Integer> ans = new ArrayList<>();
49-
robot(root, ans, 0);
50-
return ans;
51-
}
52-
53-
private void robot(TreeNode root, List<Integer> ans, int level) {
54-
if (root == null) {
55-
return;
56-
}
57-
if (ans.size() <= level) {
58-
ans.add(root.val);
84+
if (root == null) return Collections.emptyList();
85+
Deque<TreeNode> q = new ArrayDeque<>();
86+
q.offer(root);
87+
List<Integer> res = new ArrayList<>();
88+
while (!q.isEmpty()) {
89+
int size = q.size();
90+
res.add(q.peek().val);
91+
while (size-- > 0) {
92+
TreeNode node = q.poll();
93+
if (node.right != null) q.offer(node.right);
94+
if (node.left != null) q.offer(node.left);
95+
}
5996
}
60-
ans.set(level, root.val);
61-
robot(root.left, ans, level + 1);
62-
robot(root.right, ans, level + 1);
97+
return res;
6398
}
6499
}
65100
```
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,33 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* public class TreeNode {
4+
* int val;
5+
* TreeNode left;
6+
* TreeNode right;
7+
* TreeNode() {}
8+
* TreeNode(int val) { this.val = val; }
9+
* TreeNode(int val, TreeNode left, TreeNode right) {
10+
* this.val = val;
11+
* this.left = left;
12+
* this.right = right;
13+
* }
14+
* }
15+
*/
116
class Solution {
217
public List<Integer> rightSideView(TreeNode root) {
3-
List<Integer> ans = new ArrayList<>();
4-
robot(root, ans, 0);
5-
return ans;
6-
}
7-
8-
private void robot(TreeNode root, List<Integer> ans, int level) {
9-
if (root == null) {
10-
return;
11-
}
12-
if (ans.size() <= level) {
13-
ans.add(root.val);
18+
if (root == null) return Collections.emptyList();
19+
Deque<TreeNode> q = new ArrayDeque<>();
20+
q.offer(root);
21+
List<Integer> res = new ArrayList<>();
22+
while (!q.isEmpty()) {
23+
int size = q.size();
24+
res.add(q.peek().val);
25+
while (size-- > 0) {
26+
TreeNode node = q.poll();
27+
if (node.right != null) q.offer(node.right);
28+
if (node.left != null) q.offer(node.left);
29+
}
1430
}
15-
ans.set(level, root.val);
16-
robot(root.left, ans, level + 1);
17-
robot(root.right, ans, level + 1);
31+
return res;
1832
}
19-
}
33+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Definition for a binary tree node.
2+
# class TreeNode:
3+
# def __init__(self, val=0, left=None, right=None):
4+
# self.val = val
5+
# self.left = left
6+
# self.right = right
7+
class Solution:
8+
def rightSideView(self, root: TreeNode) -> List[int]:
9+
if not root:
10+
return []
11+
q = [root]
12+
res = []
13+
while q:
14+
size = len(q)
15+
res.append(q[0].val)
16+
for _ in range(size):
17+
node = q.pop(0)
18+
if node.right:
19+
q.append(node.right)
20+
if node.left:
21+
q.append(node.left)
22+
return res

0 commit comments

Comments
 (0)