-
Notifications
You must be signed in to change notification settings - Fork 0
Lowest Common Ancestor of a Binary Search Tree #29
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
+68
−0
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
36 changes: 36 additions & 0 deletions
36
pullrequests/lowest_common_ancester_of_a_binary_search_tree/step1.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,36 @@ | ||
| //lint:file-ignore U1000 Ignore all unused code | ||
| package lowestcommonancesterofabinarysearchtree | ||
|
|
||
| type TreeNode struct { | ||
| Val int | ||
| Left *TreeNode | ||
| Right *TreeNode | ||
| } | ||
|
|
||
| /* | ||
| 時間:6分30秒 | ||
|
|
||
| ルートから見ていく時、共通の祖先になっていないときは必ず、pとqは左か右のどちらかの同じ部分木にいるはず。pとqの間の値になったら共通の祖先になったとわかる。 | ||
|
|
||
| 本来は見つからなかった場合は返り値としてerrorを返したかったのですが、LeetCodeの制約上変えられないのでnilを返すようにしています。 | ||
|
|
||
| 該当ノードが見つからなかったからといってプログラムの実行の継続が困難になるわけではないと思うので、panicやlog.Panicを使うのはやり過ぎだと思います。 | ||
| 場合によっては絶対に見つからないことが起こる入力はしないはずだと言える状況であればlog.Fatalを使ってログに書き込んだ後にos.Exit(1)を呼び出してプログラムを終了させるのが良い可能性もありますが、それも通常の場合やりすぎな気がします。 | ||
| 他にはlog.Printなどを使ってログに見つからなかったことを記録しても良いかもしれませんが、見つからないことが起こる入力があり得るのであれば単にerrorを返り値として返して呼び出し側で処理するのが良いと思っています。 | ||
| 他には単に見つからなかった場合はnilを返すなども手としてはあると思いますが、そうする場合は呼び出し側にもわかるようにコメント等で書いておいて欲しいなと思います。 | ||
| */ | ||
| func lowestCommonAncestorIterativeStep1(root, p, q *TreeNode) *TreeNode { | ||
| node := root | ||
| for node != nil { | ||
| if p.Val <= node.Val && node.Val <= q.Val || q.Val <= node.Val && node.Val <= p.Val { | ||
| return node | ||
| } | ||
| if p.Val < node.Val && q.Val < node.Val { | ||
| node = node.Left | ||
| } | ||
| if node.Val < p.Val && node.Val < q.Val { | ||
| node = node.Right | ||
| } | ||
| } | ||
| return nil | ||
| } | ||
32 changes: 32 additions & 0 deletions
32
pullrequests/lowest_common_ancester_of_a_binary_search_tree/step2.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,32 @@ | ||
| //lint:file-ignore U1000 Ignore all unused code | ||
| package lowestcommonancesterofabinarysearchtree | ||
|
|
||
| /* | ||
| より見やすくなるようにリファクタしました。また、再帰を使った実装もしてみました。 | ||
| エラー処理についてはStep1と同様です。 | ||
| */ | ||
| func lowestCommonAncestorIterative(root, p, q *TreeNode) *TreeNode { | ||
| node := root | ||
| for node != nil { | ||
| if p.Val < node.Val && q.Val < node.Val { | ||
| node = node.Left | ||
| continue | ||
rihib marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| if node.Val < p.Val && node.Val < q.Val { | ||
rihib marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| node = node.Right | ||
| continue | ||
| } | ||
| return node | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| func lowestCommonAncestorRecursive(root, p, q *TreeNode) *TreeNode { | ||
| if p.Val < root.Val && q.Val < root.Val { | ||
| return lowestCommonAncestorRecursive(root.Left, p, q) | ||
| } | ||
| if root.Val < p.Val && root.Val < q.Val { | ||
| return lowestCommonAncestorRecursive(root.Right, p, q) | ||
| } | ||
| return root | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.