-
-
Notifications
You must be signed in to change notification settings - Fork 8.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: update leetcode solutions: No.0199. Binary Tree Right Side View
- Loading branch information
Showing
7 changed files
with
183 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 29 additions & 15 deletions
44
solution/0100-0199/0199.Binary Tree Right Side View/Solution.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
22
solution/0100-0199/0199.Binary Tree Right Side View/Solution.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |