-
Notifications
You must be signed in to change notification settings - Fork 0
102. Binary Tree Level Order Traversal #26
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
Open
hiroki-horiguchi-dev
wants to merge
1
commit into
main
Choose a base branch
from
tree-bst-102
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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,118 @@ | ||
| - [102. Binary Tree Level Order Traversal](https://leetcode.com/problems/binary-tree-level-order-traversal/description/) | ||
| - 方針 | ||
| - Queue | ||
| - 階層ごとにリストに node.val を詰めていけば良いと考えた | ||
| - 時間計算量: O(N), 空間計算量: O(N) | ||
| - 実装時間: 1st 20分、2nd 20分 | ||
| - 再帰関数 | ||
| - 2000程度なのでコールスタックも耐えられそうだけど、解法が思いつかない | ||
| - 左右に分離していく時に同じ階層の要素を揃えて取得するコードを書くのは難しそう。。 | ||
| - Stack | ||
| - Queue と同じような実装になりそう。Queue 実装で今日は時間切れ。 | ||
|
|
||
| ## BFS | ||
| ```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<List<Integer>> levelOrder(TreeNode root) { | ||
| List<List<Integer>> result = new ArrayList<>(); | ||
|
|
||
| if (root == null) { | ||
| return result; | ||
| } | ||
|
|
||
| Deque<TreeNodeAndDepth> nodes = new ArrayDeque<>(); | ||
| int depth = 1; | ||
| nodes.addLast(new TreeNodeAndDepth(root, 1)); | ||
|
|
||
| while (!nodes.isEmpty()) { | ||
| List<Integer> temp = new ArrayList<>(); | ||
| while (nodes.peekFirst() != null | ||
| && nodes.peekFirst().depth == depth) { | ||
| TreeNodeAndDepth nodeAndDepth = nodes.pollFirst(); | ||
| TreeNode node = nodeAndDepth.treeNode; | ||
| temp.add(node.val); | ||
|
|
||
| if (node.left != null) { | ||
| nodes.add(new TreeNodeAndDepth( | ||
| node.left, nodeAndDepth.depth + 1) | ||
| ); | ||
| } | ||
|
|
||
| if (node.right != null) { | ||
| nodes.add(new TreeNodeAndDepth(node.right, | ||
| nodeAndDepth.depth + 1)); | ||
| } | ||
| } | ||
| depth++; | ||
| result.add(temp); | ||
| } | ||
|
|
||
| return result; | ||
| } | ||
|
|
||
| private record TreeNodeAndDepth(TreeNode treeNode, int depth){} | ||
| } | ||
| ``` | ||
| - もう少し綺麗にしたい、特に depth を外に持つ必要はない | ||
| - 取り出す時、queue に存在するサイズ分二重ループを回せば良いだけだった | ||
| - record も不要 | ||
| ```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<List<Integer>> levelOrder(TreeNode root) { | ||
| List<List<Integer>> result = new ArrayList<>(); | ||
| if (root == null) { | ||
| return result; | ||
| } | ||
| Deque<TreeNode> nodes = new ArrayDeque<>(); | ||
| nodes.addLast(root); | ||
| while (!nodes.isEmpty()) { | ||
| int size = nodes.size(); | ||
| List<Integer> tempList = new ArrayList<>(); | ||
|
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. 自分なら valsInLevel と名付けると思います。 vals でも通じると思います。 |
||
| for (int i = 0; i < size; i++) { | ||
| TreeNode node = nodes.pollFirst(); | ||
| tempList.add(node.val); | ||
| if (node.left != null) { | ||
| nodes.add(node.left); | ||
| } | ||
|
|
||
| if (node.right != null) { | ||
| nodes.add(node.right); | ||
| } | ||
| } | ||
| result.add(tempList); | ||
| } | ||
|
|
||
| return result; | ||
| } | ||
| } | ||
| ``` | ||
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.
インデントが深すぎるかと思います。
Uh oh!
There was an error while loading. Please reload this page.
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.
ありがとうございます、そうですね。
2ndでだいぶ削れたので、修正できている認識です。
上記↑じゃなくて、シンプルにインデントが深いって話ですかね。。
であれば、leetcode のサイト上で解いたので条件箇所と分けるために一段インデント深くしてました。
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.
後者の意味でした。以下のようにすると良いと思います。