-
Notifications
You must be signed in to change notification settings - Fork 0
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
Convert Sorted Array to Binary Search Tree #54
Open
rihib
wants to merge
1
commit into
main
Choose a base branch
from
convert_sorted_array_to_binary_search_tree
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
31 changes: 31 additions & 0 deletions
31
pullrequests/convert_sorted_array_to_binary_search_tree/step1.go
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,31 @@ | ||
//lint:file-ignore U1000 Ignore all unused code | ||
package convertsortedarraytobinarysearchtree | ||
|
||
type TreeNode struct { | ||
Val int | ||
Left *TreeNode | ||
Right *TreeNode | ||
} | ||
|
||
/* | ||
レビュワーの方へ: | ||
- このコードは既にGoの標準のフォーマッタで整形済みです。演算子の周りにスペースがあったりなかったりしますが、これはGoのフォーマッタによるもので、優先順位の高い演算子の周りにはスペースが入らず、低い演算子の周りには入るようになっています。https://qiita.com/tchssk/items/77030b4271cd192d0347 | ||
*/ | ||
|
||
/* | ||
時間:10分 | ||
最初に解法を思いつくのに時間がかかったが、BSTはノードの値がleft < root < rightということを思い出して解くことができた。 | ||
|
||
Goではスライスはポインタをずらすだけなのでメモリコピーは発生しないという認識。 | ||
*/ | ||
func sortedArrayToBST(nums []int) *TreeNode { | ||
if len(nums) == 0 { | ||
return nil | ||
} | ||
midIndex := len(nums) / 2 | ||
return &TreeNode{ | ||
Val: nums[midIndex], | ||
Left: sortedArrayToBST(nums[:midIndex]), | ||
Right: sortedArrayToBST(nums[midIndex+1:]), | ||
} | ||
} |
112 changes: 112 additions & 0 deletions
112
pullrequests/convert_sorted_array_to_binary_search_tree/step2.go
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,112 @@ | ||
//lint:file-ignore U1000 Ignore all unused code | ||
package convertsortedarraytobinarysearchtree | ||
|
||
import "container/list" | ||
|
||
/* | ||
レビュワーの方へ: | ||
- このコードは既にGoの標準のフォーマッタで整形済みです。演算子の周りにスペースがあったりなかったりしますが、これはGoのフォーマッタによるもので、優先順位の高い演算子の周りにはスペースが入らず、低い演算子の周りには入るようになっています。https://qiita.com/tchssk/items/77030b4271cd192d0347 | ||
*/ | ||
|
||
/* | ||
他の人の解法を見て、Helper関数を使った変形や、イテレーティブにBFSやDFSで解く方法も実装してみた。 | ||
*/ | ||
func sortedArrayToBSTClosed(nums []int) *TreeNode { | ||
if len(nums) == 0 { | ||
return nil | ||
} | ||
return sortedArrayToBSTClosedHelper(nums, 0, len(nums)-1) | ||
} | ||
|
||
func sortedArrayToBSTClosedHelper(nums []int, left, right int) *TreeNode { | ||
if left > right { | ||
return nil | ||
} | ||
mid := left + (right-left)/2 | ||
return &TreeNode{ | ||
Val: nums[mid], | ||
Left: sortedArrayToBSTClosedHelper(nums, left, mid-1), | ||
Right: sortedArrayToBSTClosedHelper(nums, mid+1, right), | ||
} | ||
} | ||
|
||
func sortedArrayToBSTHalfOpen(nums []int) *TreeNode { | ||
if len(nums) == 0 { | ||
return nil | ||
} | ||
return sortedArrayToBSTHalfOpenHelper(nums, 0, len(nums)) | ||
} | ||
|
||
func sortedArrayToBSTHalfOpenHelper(nums []int, left, right int) *TreeNode { | ||
if left == right { | ||
return nil | ||
} | ||
mid := left + (right-left)/2 | ||
return &TreeNode{ | ||
Val: nums[mid], | ||
Left: sortedArrayToBSTHalfOpenHelper(nums, left, mid), | ||
Right: sortedArrayToBSTHalfOpenHelper(nums, mid+1, right), | ||
} | ||
} | ||
|
||
type bstElement struct { | ||
root *TreeNode | ||
left []int | ||
right []int | ||
} | ||
|
||
func sortedArrayToBSTDFS(nums []int) *TreeNode { | ||
if len(nums) == 0 { | ||
return nil | ||
} | ||
midIndex := len(nums) / 2 | ||
root := &TreeNode{Val: nums[midIndex]} | ||
stack := []bstElement{{root, nums[:midIndex], nums[midIndex+1:]}} | ||
for len(stack) > 0 { | ||
e := stack[len(stack)-1] | ||
stack = stack[:len(stack)-1] | ||
if len(e.left) > 0 { | ||
leftMid := len(e.left) / 2 | ||
e.root.Left = &TreeNode{Val: e.left[leftMid]} | ||
stack = append(stack, bstElement{ | ||
e.root.Left, e.left[:leftMid], e.left[leftMid+1:], | ||
}) | ||
} | ||
if len(e.right) > 0 { | ||
rightMid := len(e.right) / 2 | ||
e.root.Right = &TreeNode{Val: e.right[rightMid]} | ||
stack = append(stack, bstElement{ | ||
e.root.Right, e.right[:rightMid], e.right[rightMid+1:], | ||
}) | ||
} | ||
} | ||
return root | ||
} | ||
|
||
func sortedArrayToBSTBFS(nums []int) *TreeNode { | ||
if len(nums) == 0 { | ||
return nil | ||
} | ||
midIndex := len(nums) / 2 | ||
root := &TreeNode{Val: nums[midIndex]} | ||
queue := list.New() | ||
queue.PushBack(bstElement{root, nums[:midIndex], nums[midIndex+1:]}) | ||
for queue.Len() > 0 { | ||
e := queue.Remove(queue.Front()).(bstElement) | ||
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. container/listを使ったことがなかったので質問させてください
|
||
if len(e.left) > 0 { | ||
leftMid := len(e.left) / 2 | ||
e.root.Left = &TreeNode{Val: e.left[leftMid]} | ||
queue.PushBack(bstElement{ | ||
e.root.Left, e.left[:leftMid], e.left[leftMid+1:], | ||
}) | ||
} | ||
if len(e.right) > 0 { | ||
rightMid := len(e.right) / 2 | ||
e.root.Right = &TreeNode{Val: e.right[rightMid]} | ||
queue.PushBack(bstElement{ | ||
e.root.Right, e.right[:rightMid], e.right[rightMid+1:], | ||
}) | ||
} | ||
} | ||
return root | ||
} |
36 changes: 36 additions & 0 deletions
36
pullrequests/convert_sorted_array_to_binary_search_tree/step3.go
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,36 @@ | ||
//lint:file-ignore U1000 Ignore all unused code | ||
package convertsortedarraytobinarysearchtree | ||
|
||
/* | ||
レビュワーの方へ: | ||
- このコードは既にGoの標準のフォーマッタで整形済みです。演算子の周りにスペースがあったりなかったりしますが、これはGoのフォーマッタによるもので、優先順位の高い演算子の周りにはスペースが入らず、低い演算子の周りには入るようになっています。https://qiita.com/tchssk/items/77030b4271cd192d0347 | ||
*/ | ||
|
||
/* | ||
その他にも最初にdummyノードで完全二分木を構築してから、In-orer traversalで値をセットしていくという方法で解いている人もいたので、同様に実装してみた。 | ||
*/ | ||
func sortedArrayToBSTInorder(nums []int) *TreeNode { | ||
root := dummyCompleteBinaryTree(0, len(nums)) | ||
setValueInorder(root, nums) | ||
return root | ||
} | ||
|
||
func dummyCompleteBinaryTree(index, length int) *TreeNode { | ||
if index >= length { | ||
return nil | ||
} | ||
node := &TreeNode{} | ||
node.Left = dummyCompleteBinaryTree(index*2+1, length) | ||
node.Right = dummyCompleteBinaryTree(index*2+2, length) | ||
return node | ||
} | ||
|
||
func setValueInorder(root *TreeNode, nums []int) []int { | ||
if root == nil || len(nums) == 0 { | ||
return nums | ||
} | ||
nums = setValueInorder(root.Left, nums) | ||
root.Val = nums[0] | ||
nums = setValueInorder(root.Right, nums[1:]) | ||
return nums | ||
} |
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.
この stack queue を使った実装 TreeNode の初期化が重複している感覚があります。
ポインターのポインターを使うとなくせますね。
https://discord.com/channels/1084280443945353267/1262688866326941718/1298575468353556501