-
-
Notifications
You must be signed in to change notification settings - Fork 250
[njngwn] Week 14 Solutions #1950
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,43 @@ | ||
| /** | ||
| * 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 { | ||
| // Time Complexity: O(n), n: the number of nodes | ||
| // Space Complexity: O(w), w: max width of tree | ||
|
|
||
| public List<List<Integer>> levelOrder(TreeNode root) { | ||
| List<List<Integer>> results = new ArrayList<>(); | ||
| if (root == null) return results; | ||
|
|
||
| Queue<TreeNode> levelQueue = new LinkedList<>(); // use queue considering FIFO | ||
| levelQueue.offer(root); | ||
|
|
||
| while (!levelQueue.isEmpty()) { | ||
| ArrayList<Integer> level = new ArrayList<>(); // node values list for each level | ||
| int size = levelQueue.size(); | ||
|
|
||
| for (int i = 0; i < size; ++i) { | ||
| TreeNode node = levelQueue.poll(); | ||
| level.add(node.val); | ||
| if(node.left != null) levelQueue.offer(node.left); | ||
| if(node.right != null) levelQueue.offer(node.right); | ||
| } | ||
|
|
||
| results.add(level); | ||
| } | ||
|
|
||
| return results; | ||
| } | ||
| } | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. bit operations 를 사용해서 풀이해 보시면 어떨까요! |
This file contains hidden or 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,11 @@ | ||
| class Solution { | ||
| public int[] countBits(int n) { | ||
| int[] bitCountArr = new int[n+1]; | ||
|
|
||
| for (int i = 1; i <= n; ++i) { | ||
| bitCountArr[i] = bitCountArr[i/2] + (i & 1); | ||
| } | ||
|
|
||
| return bitCountArr; | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Queue라는 컬렉션(? 자바에서는 컬렉션이라는 명칭을 사용했던거 같아 사용해봅니다)에서 데이터를 넣을 때, enqueue가 아닌 offer 메서드를 사용하는군요. 아니면
new LinkedList()를 사용해서 그런건가요?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
자바에는 Queue가 LinkedList로 구현되어 있는걸로 알고있구요. offer 메소드 사용합니다!