-
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
Same Tree #45
base: main
Are you sure you want to change the base?
Conversation
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.
よいと思います!
@@ -0,0 +1,29 @@ | |||
//lint:file-ignore U1000 Ignore all unused code |
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.
検索を効率化するために例えば下記のように木全体をハッシュ化するなどはいかがでしょうか。
- 木の追加時にノードの値を文字列に変換して、pre-orderでそれらの文字列を結合していき、最終的に得られた文字列を衝突耐性の高いハッシュ関数(SHA256など)でハッシュ化
- ノードがない箇所については"\nil"のような文字列を入れることで表現する。入力に\が含まれているときは\\にするなどしてエスケープ処理する
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.
この問題とは関係ないですが、木の全ての部分木もハッシュ化したいならどうしますか?全ての部分木に対してpre-order traversalをするのは効率が悪そうです。
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.
post-orderで下から順に各ノードでハッシュを計算し、親ノードは左右子ノードのハッシュ値を使ってハッシュを計算するという形にするのが良さそうな気がしました。
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.
in-orderではないですか?
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.
すみません。post-orderですね。
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.
ありがとうございます。全く考えてなかったことなので勉強になりました。
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.
Merkle treeというのがあるのですね、、
/* | ||
リファクタした。 | ||
*/ | ||
func isSameTreeStep2(p *TreeNode, q *TreeNode) bool { |
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.
Iterative DFS/BFSでも書けますか?
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.
再帰をループに直すのは、練習としてわりと重要度高いと個人的には思っています。機会があれば、手の運動くらいの気持ちで。
Same Treeを解きました。レビューをお願い致します。
問題:https://leetcode.com/problems/same-tree/
言語:Go
すでに解いている方々:
none