-
Notifications
You must be signed in to change notification settings - Fork 0
102. Binary Tree Level Order Traversal #25
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
hroc135
wants to merge
3
commits into
main
Choose a base branch
from
102BinaryTreeLevelOrderTraversal
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
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,5 @@ | ||
| start_new_problem.sh | ||
| main.go | ||
| go.mod | ||
| go.sum | ||
| *.go |
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,152 @@ | ||
|
|
||
| ### Step 1 | ||
| - 方針: BFSを使って木をレベル毎に探索していく | ||
| - アルゴリズムを言語化: レベルiのノードをスライスcurrentLevelNodesで保持する。 | ||
| currentLevelNodesを順に舐めてレベルi+1のノードをnextLevelNodesに、ノードの値をnextLevelNodeValuesに格納する。 | ||
| レベルi+1にノードがなければループから出て結果を返す。 | ||
| レベルi+1のノードがあればそれを結果に加え、currentLevelNodesをnextLevelNodesに更新する(レベルiをi+1に更新する)。 | ||
| - 20分弱でできた(2回テストケースに引っかかった) | ||
| - 次のレベルにノードがない時に空リストが答えの最後に追加されてしまっていた | ||
| - 一番外側のループに条件を加えなかったのでrootがnilでも動くかと思ったが、 | ||
| root.Valにアクセスしていたため、エラー | ||
| - 時間計算量: O(n) | ||
| - 空間計算量: O(n) | ||
|
|
||
| ```Go | ||
| func levelOrder(root *TreeNode) [][]int { | ||
| if root == nil { | ||
| return [][]int{} | ||
| } | ||
| nodeValuesByLevel := make([][]int, 0, 2000) | ||
| nodeValuesByLevel = append(nodeValuesByLevel, []int{root.Val}) | ||
| currentLevelNodes := []*TreeNode{root} | ||
| for { | ||
| nextLevelNodes := []*TreeNode{} | ||
| nextLevelNodeValues := []int{} | ||
| for _, node := range currentLevelNodes { | ||
| if node.Left != nil { | ||
| nextLevelNodes = append(nextLevelNodes, node.Left) | ||
| nextLevelNodeValues = append(nextLevelNodeValues, node.Left.Val) | ||
| } | ||
| if node.Right != nil { | ||
| nextLevelNodes = append(nextLevelNodes, node.Right) | ||
| nextLevelNodeValues = append(nextLevelNodeValues, node.Right.Val) | ||
| } | ||
| } | ||
| if len(nextLevelNodes) == 0 { | ||
| break | ||
| } | ||
| nodeValuesByLevel = append(nodeValuesByLevel, nextLevelNodeValues) | ||
| currentLevelNodes = nextLevelNodes | ||
| } | ||
| return nodeValuesByLevel | ||
| } | ||
| ``` | ||
|
|
||
| ### Step 2 | ||
| #### 2a BFS2 | ||
| - 参考: https://github.com/hayashi-ay/leetcode/pull/32/files#diff-f64e64b98ee3e79b1af4864eb48c186566221d5e613381a9102b5069412dd01eR9 | ||
| - アルゴリズムを言語化: レベルiのノードをスライスcurrentLevelNodesで保持する。 | ||
| currentLevelNodesを順に舐めてcurrentLevelNodesの値をcurrentLevelValuesに入れていき、 | ||
| レベルi+1のノードをnextLevelNodesに入れていく。 | ||
| currentLevelNodesを舐め終わったらcurrentLevelValuesを結果に加える。 | ||
| currentLevelNodesをレベルi+1のノード集に更新する。 | ||
| これをcurrentLevelNodesが0になるまで続ける | ||
| - こちら、今いるレベルのノードの値を答えに加え、次のレベルのノードをスライスに溜めていく、 | ||
| という作業方針の方が、step1の次のレベルのノードの値を答えに加える、という方針より直感的だと思った。 | ||
|
|
||
| ```Go | ||
| func levelOrder(root *TreeNode) [][]int { | ||
| if root == nil { | ||
| return [][]int{} | ||
| } | ||
| nodeValuesByLevel := [][]int{} | ||
| currentLevelNodes := []*TreeNode{root} | ||
| for len(currentLevelNodes) > 0 { | ||
| currentLevelValues := []int{} | ||
| nextLevelNodes := []*TreeNode{} | ||
| for _, node := range currentLevelNodes { | ||
| currentLevelValues = append(currentLevelValues, node.Val) | ||
| if node.Left != nil { | ||
| nextLevelNodes = append(nextLevelNodes, node.Left) | ||
| } | ||
| if node.Right != nil { | ||
| nextLevelNodes = append(nextLevelNodes, node.Right) | ||
| } | ||
| } | ||
| nodeValuesByLevel = append(nodeValuesByLevel, currentLevelValues) | ||
| currentLevelNodes = nextLevelNodes | ||
| } | ||
| return nodeValuesByLevel | ||
| } | ||
| ``` | ||
|
|
||
| #### 2b スタックDFS | ||
| - 問題文中の"from left to right"を見落としていてWrong Answerになってしまった。 | ||
|
|
||
| - DFSだとレベルを保持するための構造体を作る必要が生じてしまうので、2aのBFSの解答の方が適切 | ||
| ```Go | ||
| type nodeLevel struct { | ||
| node *TreeNode | ||
| level int | ||
| } | ||
|
|
||
| func levelOrder(root *TreeNode) [][]int { | ||
| if root == nil { | ||
| return [][]int{} | ||
| } | ||
| nodeValuesByLevel := [][]int{} | ||
| stack := []nodeLevel{{root, 0}} | ||
| for len(stack) > 0 { | ||
| top := stack[len(stack)-1] | ||
| node, level := top.node, top.level | ||
| stack = stack[:len(stack)-1] | ||
| for len(nodeValuesByLevel) <= level { | ||
| nodeValuesByLevel = append(nodeValuesByLevel, []int{}) | ||
| } | ||
| nodeValuesByLevel[level] = append(nodeValuesByLevel[level], node.Val) | ||
| if node.Right != nil { | ||
| stack = append(stack, nodeLevel{node.Right, level + 1}) | ||
| } | ||
| if node.Left != nil { | ||
| stack = append(stack, nodeLevel{node.Left, level + 1}) | ||
| } | ||
| } | ||
| return nodeValuesByLevel | ||
| } | ||
| ``` | ||
|
|
||
| ### Step 3 | ||
| - レベル毎に木を探索するBFSの方がDFSより自然な発想。 | ||
| BFSの中でもstep1と比べて2aの方が自然な手順(2aのアルゴリズムの説明参照)なのでこれを採用。 | ||
| - `currentLevelNodes`の要素数をnとすると、`currentLevelNodeValues`の要素数はnなので初期化時にキャパシティをnに設定。 | ||
| `nextLevelNodes`の要素数は最大で2n、最小で0。 | ||
| 今回nの最大値は2000で、2000個(< 2^11)のノードからなる綺麗な二分木は一つのレベルで最大2^9 = 512になるので、0 ~ 512だけ差があることになる。 | ||
| キャパシティを2nと指定して再割り当てをなくすか、無駄なメモリ使用を嫌って指定しないか、間を取ってnとしてみるか、が選択肢としてあるが、 | ||
| 再割り当てが起きても高々9回なので(キャパシティは2^10までは2倍ずつ増えるので)、削れる実行時間よりメモリの無駄の方が大きいと思えたので指定しないことに | ||
|
|
||
| ```Go | ||
| func levelOrder(root *TreeNode) [][]int { | ||
| if root == nil { | ||
| return [][]int{} | ||
| } | ||
| nodeValuesByLevel := [][]int{} | ||
| currentLevelNodes := []*TreeNode{root} | ||
| for len(currentLevelNodes) > 0 { | ||
| currentLevelNodeValues := make([]int, 0, len(currentLevelNodes)) | ||
| nextLevelNodes := []*TreeNode{} | ||
| for _, node := range currentLevelNodes { | ||
| currentLevelNodeValues = append(currentLevelNodeValues, node.Val) | ||
| if node.Left != nil { | ||
| nextLevelNodes = append(nextLevelNodes, node.Left) | ||
| } | ||
| if node.Right != nil { | ||
| nextLevelNodes = append(nextLevelNodes, node.Right) | ||
| } | ||
| } | ||
| nodeValuesByLevel = append(nodeValuesByLevel, currentLevelNodeValues) | ||
| currentLevelNodes = nextLevelNodes | ||
| } | ||
| return nodeValuesByLevel | ||
| } | ||
| ``` | ||
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.
マジックナンバーは定数にしたいなと思いました。
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.
ちょうど先ほど別問題で似たような指摘を受けました
定数にする or 定めない の選択肢がありますが、最大要素数が2000程度ならそんなに実行時間の短縮に効果が期待できないのでキャパシティを定めなくていいかなと思いました
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.
https://github.com/hroc135/leetcode/pull/24/files#r1808306580
リンク貼っておきます。