GeezerVison is showing github.com — feat: update leetcode solutions: No.0199. Binary Tree Right Side View · ctianjin/leetcode@b3ea654
File treeExpand file treeCollapse file tree Open diff view settings solution/0100-0199/0199.Binary Tree Right Side View
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboard +22Lines changed: 22 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 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 |
Collapse file Copy file name to clipboardExpand all lines: README.md +4-1Lines changed: 4 additions & 1 deletion - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
| 108 | 108 | - [翻转二叉树](/solution/0200-0299/0226.Invert%20Binary%20Tree/README.md) | 109 | 109 | - [二叉树的层次遍历](/solution/0100-0199/0102.Binary%20Tree%20Level%20Order%20Traversal/README.md) | 110 | 110 | - [二叉树的层次遍历 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) | 111 | 112 | - [二叉树的最大深度](/solution/0100-0199/0104.Maximum%20Depth%20of%20Binary%20Tree/README.md) | 112 | 113 | - [二叉树的最小深度](/solution/0100-0199/0111.Minimum%20Depth%20of%20Binary%20Tree/README.md) | 113 | 114 | - [二叉树的所有路径](/solution/0200-0299/0257.Binary%20Tree%20Paths/README.md) | | 190 | 191 |
| 191 | 192 | 非常感谢以下所有朋友对本项目的贡献,你们是最可爱的人! | 192 | 193 |
| 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> | 194 | 197 |
| 195 | 198 | ## 赞助者 | 196 | 199 |
| |
Collapse file Copy file name to clipboardExpand all lines: README_EN.md +4-1Lines changed: 4 additions & 1 deletion - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -105,6 +105,7 @@ Complete solutions to [LeetCode](https://leetcode-cn.com/problemset/all/), [LCOF | 105 | 105 | - [Invert Binary Tree](/solution/0200-0299/0226.Invert%20Binary%20Tree/README_EN.md) | 106 | 106 | - [Binary Tree Level Order Traversal](/solution/0100-0199/0102.Binary%20Tree%20Level%20Order%20Traversal/README_EN.md) | 107 | 107 | - [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) | 108 | 109 | - [Maximum Depth of Binary Tree](/solution/0100-0199/0104.Maximum%20Depth%20of%20Binary%20Tree/README_EN.md) | 109 | 110 | - [Minimum Depth of Binary Tree](/solution/0100-0199/0111.Minimum%20Depth%20of%20Binary%20Tree/README_EN.md) | 110 | 111 | - [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 | 180 | 181 |
| 181 | 182 | This project exists thanks to all the people who contribute. | 182 | 183 |
| 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> | 184 | 187 |
| 185 | 188 | ## Backers & Sponsors | 186 | 189 |
| |
Collapse file Copy file name to clipboardExpand all lines: solution/0100-0199/0199.Binary Tree Right Side View/README.md +52-15Lines changed: 52 additions & 15 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
| 24 | 24 |
| 25 | 25 | <!-- 这里可写通用的实现逻辑 --> | 26 | 26 |
| | 27 | +队列实现。 | | 28 | + | 27 | 29 | <!-- tabs:start --> | 28 | 30 |
| 29 | 31 | ### **Python3** | 30 | 32 |
| 31 | 33 | <!-- 这里可写当前语言的特殊实现逻辑 --> | 32 | 34 |
| 33 | 35 | ```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 | 35 | 58 | ``` | 36 | 59 |
| 37 | 60 | ### **Java** | 38 | 61 |
| 39 | 62 | <!-- 这里可写当前语言的特殊实现逻辑 --> | 40 | 63 |
| 41 | 64 | ```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 | + */ | 42 | 80 | class Solution { | 43 | 81 | 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 | + } | 55 | 94 | } | 56 | | - ans.set(level, root.val); | 57 | | - robot(root.left, ans, level + 1); | 58 | | - robot(root.right, ans, level + 1); | | 95 | + return res; | 59 | 96 | } | 60 | 97 | } | 61 | 98 | ``` | |
Collapse file Copy file name to clipboardExpand all lines: solution/0100-0199/0199.Binary Tree Right Side View/README_EN.md +50-15Lines changed: 50 additions & 15 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
| 37 | 37 | ### **Python3** | 38 | 38 |
| 39 | 39 | ```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 | 41 | 62 | ``` | 42 | 63 |
| 43 | 64 | ### **Java** | 44 | 65 |
| 45 | 66 | ```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 | + */ | 46 | 82 | class Solution { | 47 | 83 | 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 | + } | 59 | 96 | } | 60 | | - ans.set(level, root.val); | 61 | | - robot(root.left, ans, level + 1); | 62 | | - robot(root.right, ans, level + 1); | | 97 | + return res; | 63 | 98 | } | 64 | 99 | } | 65 | 100 | ``` | |
Collapse file Copy file name to clipboard +29-15Lines changed: 29 additions & 15 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 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 | + */ | 1 | 16 | class Solution { | 2 | 17 | 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 | + } | 14 | 30 | } | 15 | | - ans.set(level, root.val); | 16 | | - robot(root.left, ans, level + 1); | 17 | | - robot(root.right, ans, level + 1); | | 31 | + return res; | 18 | 32 | } | 19 | | -} | | 33 | +} |
Collapse file Copy file name to clipboard +22Lines changed: 22 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 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 |
|