-
-
Notifications
You must be signed in to change notification settings - Fork 305
[rivkode] WEEK 14 Solutions #1955
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 1 commit
Commits
Show all changes
3 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,82 @@ | ||
| /* | ||
| BFS를 사용한 이유는 depth 가 기준이 되어야 하기 때문이다. | ||
| depth 별로 value값을 넣어야하기 때문에 BFS로 탐색하며 해당하는 모든 값들을 List에 넣어주었다. | ||
| 이때 포인트는 몇개의 값들을 depth 별로 나눌것인가 ? 인데 왜냐하면 queue는 자식 노드들을 탐색하며 지속적으로 추가되기 때문이다. | ||
| 그래서 2중으로 loop를 돌아야 한다. | ||
| 첫번째는 while문으로 queue가 empty될때까지 확인하는 loop이고 | ||
| 두번째는 queue 사이즈를 미리 계산하여 특정 level의 개수 만큼 도는 for loop 이다. | ||
| 이렇게 2번 loop를 돌면 depth 별로 size를 알 수 있게된다. | ||
| */ | ||
|
|
||
| /** | ||
| * 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; | ||
| * } | ||
| * } | ||
| */ | ||
| import java.util.*; | ||
|
|
||
| class Solution { | ||
| public List<List<Integer>> levelOrder(TreeNode root) { | ||
| Queue<TreeNode> queue = new LinkedList<>(); | ||
| List<List<Integer>> result = new ArrayList<>(); | ||
|
|
||
| if (root == null) { | ||
| return new ArrayList<>(); | ||
| } | ||
|
|
||
| queue.offer(root); | ||
|
|
||
| while(!queue.isEmpty()) { | ||
| List<Integer> levelList = new ArrayList<>(); | ||
| int queueSize = queue.size(); | ||
|
|
||
| for (int i=0; i<queueSize; i++) { | ||
| TreeNode node = queue.poll(); | ||
| if (node == null) { | ||
| continue; | ||
| } | ||
|
|
||
| levelList.add(node.val); | ||
|
|
||
| if (node.left != null) { | ||
| queue.offer(node.left); | ||
| } | ||
|
|
||
| if (node.right != null) { | ||
| queue.offer(node.right); | ||
| } | ||
| } | ||
| result.add(levelList); | ||
| } | ||
|
|
||
| return result; | ||
| } | ||
|
|
||
| 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; | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
||
|
|
||
|
|
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.
제가 문제를 안 읽어서... poll, offer라는 함수를 쓰는 걸 처음 봤네요. 반환, 추가의 역할을 하나요?
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.
네 맞아요 poll은 Queue 자료구조에서 가장 첫번째로 들어온 요소를 반환하고 Queue에서 제거합니다. offer은 가장 마지막에 추가하는 역할을 합니다