Skip to content

Commit

Permalink
feat: update leetcode solutions: No.0199. Binary Tree Right Side View
Browse files Browse the repository at this point in the history
  • Loading branch information
yanglbme committed Mar 28, 2021
1 parent 692acff commit b3ea654
Show file tree
Hide file tree
Showing 7 changed files with 183 additions and 47 deletions.
22 changes: 22 additions & 0 deletions .github/workflows/contributors.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: Contributors

on:
schedule:
- cron: "0/10 * * * *"
push:
branches:
- main

jobs:
contributors:
runs-on: ubuntu-latest
if: github.repository == 'doocs/leetcode'
steps:
- uses: bubkoo/contributors-list@v1
name: contributors-doocs-leetcode
with:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
svgPath: images/contributors.svg
svgWidth: 890
round: true
includeBots: true
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@
- [翻转二叉树](/solution/0200-0299/0226.Invert%20Binary%20Tree/README.md)
- [二叉树的层次遍历](/solution/0100-0199/0102.Binary%20Tree%20Level%20Order%20Traversal/README.md)
- [二叉树的层次遍历 II](/solution/0100-0199/0107.Binary%20Tree%20Level%20Order%20Traversal%20II/README.md)
- [二叉树的右视图](/solution/0100-0199/0199.Binary%20Tree%20Right%20Side%20View/README.md)
- [二叉树的最大深度](/solution/0100-0199/0104.Maximum%20Depth%20of%20Binary%20Tree/README.md)
- [二叉树的最小深度](/solution/0100-0199/0111.Minimum%20Depth%20of%20Binary%20Tree/README.md)
- [二叉树的所有路径](/solution/0200-0299/0257.Binary%20Tree%20Paths/README.md)
Expand Down Expand Up @@ -190,7 +191,9 @@

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

<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>
<!-- https://opencollective.com/doocs-leetcode/contributors.svg?width=890&button=false -->

<a href="https://github.com/doocs/leetcode/graphs/contributors" target="_blank"><img src="./images/contributors.svg" /></a>

## 赞助者

Expand Down
5 changes: 4 additions & 1 deletion README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ Complete solutions to [LeetCode](https://leetcode-cn.com/problemset/all/), [LCOF
- [Invert Binary Tree](/solution/0200-0299/0226.Invert%20Binary%20Tree/README_EN.md)
- [Binary Tree Level Order Traversal](/solution/0100-0199/0102.Binary%20Tree%20Level%20Order%20Traversal/README_EN.md)
- [Binary Tree Level Order Traversal II](/solution/0100-0199/0107.Binary%20Tree%20Level%20Order%20Traversal%20II/README_EN.md)
- [Binary Tree Right Side View](/solution/0100-0199/0199.Binary%20Tree%20Right%20Side%20View/README_EN.md)
- [Maximum Depth of Binary Tree](/solution/0100-0199/0104.Maximum%20Depth%20of%20Binary%20Tree/README_EN.md)
- [Minimum Depth of Binary Tree](/solution/0100-0199/0111.Minimum%20Depth%20of%20Binary%20Tree/README_EN.md)
- [Binary Tree Paths](/solution/0200-0299/0257.Binary%20Tree%20Paths/README_EN.md)
Expand Down Expand Up @@ -180,7 +181,9 @@ You can also contribute to [doocs/leetcode](https://github.com/doocs/leetcode) u

This project exists thanks to all the people who contribute.

<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>
<!-- https://opencollective.com/doocs-leetcode/contributors.svg?width=890&button=false -->

<a href="https://github.com/doocs/leetcode/graphs/contributors" target="_blank"><img src="./images/contributors.svg" /></a>

## Backers & Sponsors

Expand Down
67 changes: 52 additions & 15 deletions solution/0100-0199/0199.Binary Tree Right Side View/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,38 +24,75 @@

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

队列实现。

<!-- tabs:start -->

### **Python3**

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

```python

# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def rightSideView(self, root: TreeNode) -> List[int]:
if not root:
return []
q = [root]
res = []
while q:
size = len(q)
res.append(q[0].val)
for _ in range(size):
node = q.pop(0)
if node.right:
q.append(node.right)
if node.left:
q.append(node.left)
return res
```

### **Java**

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

```java
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public List<Integer> rightSideView(TreeNode root) {
List<Integer> ans = new ArrayList<>();
robot(root, ans, 0);
return ans;
}

private void robot(TreeNode root, List<Integer> ans, int level) {
if (root == null) {
return;
}
if (ans.size() <= level) {
ans.add(root.val);
if (root == null) return Collections.emptyList();
Deque<TreeNode> q = new ArrayDeque<>();
q.offer(root);
List<Integer> res = new ArrayList<>();
while (!q.isEmpty()) {
int size = q.size();
res.add(q.peek().val);
while (size-- > 0) {
TreeNode node = q.poll();
if (node.right != null) q.offer(node.right);
if (node.left != null) q.offer(node.left);
}
}
ans.set(level, root.val);
robot(root.left, ans, level + 1);
robot(root.right, ans, level + 1);
return res;
}
}
```
Expand Down
65 changes: 50 additions & 15 deletions solution/0100-0199/0199.Binary Tree Right Side View/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,29 +37,64 @@
### **Python3**

```python

# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def rightSideView(self, root: TreeNode) -> List[int]:
if not root:
return []
q = [root]
res = []
while q:
size = len(q)
res.append(q[0].val)
for _ in range(size):
node = q.pop(0)
if node.right:
q.append(node.right)
if node.left:
q.append(node.left)
return res
```

### **Java**

```java
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public List<Integer> rightSideView(TreeNode root) {
List<Integer> ans = new ArrayList<>();
robot(root, ans, 0);
return ans;
}

private void robot(TreeNode root, List<Integer> ans, int level) {
if (root == null) {
return;
}
if (ans.size() <= level) {
ans.add(root.val);
if (root == null) return Collections.emptyList();
Deque<TreeNode> q = new ArrayDeque<>();
q.offer(root);
List<Integer> res = new ArrayList<>();
while (!q.isEmpty()) {
int size = q.size();
res.add(q.peek().val);
while (size-- > 0) {
TreeNode node = q.poll();
if (node.right != null) q.offer(node.right);
if (node.left != null) q.offer(node.left);
}
}
ans.set(level, root.val);
robot(root.left, ans, level + 1);
robot(root.right, ans, level + 1);
return res;
}
}
```
Expand Down
44 changes: 29 additions & 15 deletions solution/0100-0199/0199.Binary Tree Right Side View/Solution.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,33 @@
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public List<Integer> rightSideView(TreeNode root) {
List<Integer> ans = new ArrayList<>();
robot(root, ans, 0);
return ans;
}

private void robot(TreeNode root, List<Integer> ans, int level) {
if (root == null) {
return;
}
if (ans.size() <= level) {
ans.add(root.val);
if (root == null) return Collections.emptyList();
Deque<TreeNode> q = new ArrayDeque<>();
q.offer(root);
List<Integer> res = new ArrayList<>();
while (!q.isEmpty()) {
int size = q.size();
res.add(q.peek().val);
while (size-- > 0) {
TreeNode node = q.poll();
if (node.right != null) q.offer(node.right);
if (node.left != null) q.offer(node.left);
}
}
ans.set(level, root.val);
robot(root.left, ans, level + 1);
robot(root.right, ans, level + 1);
return res;
}
}
}
22 changes: 22 additions & 0 deletions solution/0100-0199/0199.Binary Tree Right Side View/Solution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def rightSideView(self, root: TreeNode) -> List[int]:
if not root:
return []
q = [root]
res = []
while q:
size = len(q)
res.append(q[0].val)
for _ in range(size):
node = q.pop(0)
if node.right:
q.append(node.right)
if node.left:
q.append(node.left)
return res

0 comments on commit b3ea654

Please sign in to comment.