diff --git a/.github/workflows/contributors.yml b/.github/workflows/contributors.yml new file mode 100644 index 0000000000000..76c28d7a504b8 --- /dev/null +++ b/.github/workflows/contributors.yml @@ -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 diff --git a/README.md b/README.md index bd859743b5dde..eb003610786c5 100644 --- a/README.md +++ b/README.md @@ -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) @@ -190,7 +191,9 @@ 非常感谢以下所有朋友对本项目的贡献,你们是最可爱的人! - + + + ## 赞助者 diff --git a/README_EN.md b/README_EN.md index 45ed8f6317782..c6743e6b3d827 100644 --- a/README_EN.md +++ b/README_EN.md @@ -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) @@ -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. - + + + ## Backers & Sponsors diff --git a/solution/0100-0199/0199.Binary Tree Right Side View/README.md b/solution/0100-0199/0199.Binary Tree Right Side View/README.md index c89cc8556b890..46aef80e120f4 100644 --- a/solution/0100-0199/0199.Binary Tree Right Side View/README.md +++ b/solution/0100-0199/0199.Binary Tree Right Side View/README.md @@ -24,6 +24,8 @@ +队列实现。 + ### **Python3** @@ -31,7 +33,28 @@ ```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** @@ -39,23 +62,37 @@ ```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 rightSideView(TreeNode root) { - List ans = new ArrayList<>(); - robot(root, ans, 0); - return ans; - } - - private void robot(TreeNode root, List ans, int level) { - if (root == null) { - return; - } - if (ans.size() <= level) { - ans.add(root.val); + if (root == null) return Collections.emptyList(); + Deque q = new ArrayDeque<>(); + q.offer(root); + List 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; } } ``` diff --git a/solution/0100-0199/0199.Binary Tree Right Side View/README_EN.md b/solution/0100-0199/0199.Binary Tree Right Side View/README_EN.md index 06faf6fb3de09..d755e831f863d 100644 --- a/solution/0100-0199/0199.Binary Tree Right Side View/README_EN.md +++ b/solution/0100-0199/0199.Binary Tree Right Side View/README_EN.md @@ -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 rightSideView(TreeNode root) { - List ans = new ArrayList<>(); - robot(root, ans, 0); - return ans; - } - - private void robot(TreeNode root, List ans, int level) { - if (root == null) { - return; - } - if (ans.size() <= level) { - ans.add(root.val); + if (root == null) return Collections.emptyList(); + Deque q = new ArrayDeque<>(); + q.offer(root); + List 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; } } ``` diff --git a/solution/0100-0199/0199.Binary Tree Right Side View/Solution.java b/solution/0100-0199/0199.Binary Tree Right Side View/Solution.java index 8c81be4b73a10..ea9802eae602b 100644 --- a/solution/0100-0199/0199.Binary Tree Right Side View/Solution.java +++ b/solution/0100-0199/0199.Binary Tree Right Side View/Solution.java @@ -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 rightSideView(TreeNode root) { - List ans = new ArrayList<>(); - robot(root, ans, 0); - return ans; - } - - private void robot(TreeNode root, List ans, int level) { - if (root == null) { - return; - } - if (ans.size() <= level) { - ans.add(root.val); + if (root == null) return Collections.emptyList(); + Deque q = new ArrayDeque<>(); + q.offer(root); + List 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; } -} +} \ No newline at end of file diff --git a/solution/0100-0199/0199.Binary Tree Right Side View/Solution.py b/solution/0100-0199/0199.Binary Tree Right Side View/Solution.py new file mode 100644 index 0000000000000..760677369b762 --- /dev/null +++ b/solution/0100-0199/0199.Binary Tree Right Side View/Solution.py @@ -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