-
Notifications
You must be signed in to change notification settings - Fork 0
Solved Arai60/105. Construct Binary Tree from Preorder and Inorder Traversal #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
base: main
Are you sure you want to change the base?
Conversation
| - 取りあえず左と右に分けて再帰に任せる方針で実装 | ||
| - preorderの先頭が出るまでinorderで探して、出てきたら左と右で区切る | ||
| - 左は都度追加していく方が効率良さそうだが少し対称性が難あり | ||
| - left_inorderのままleft_preorderのフィルタリングをするとTLEになる |
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.
念のため確認させてください。この時の時間計算量と、おおよその処理時間はどれくらいになりますか?
| num_nodes = len(inorder) | ||
| inorder_val_to_index = {inorder[i]: i for i in range(num_nodes)} | ||
|
|
||
| def buildTreeHelper(preorder_index:int, start: int, end: int) -> Optional[TreeNode]: |
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.
: のあとにスペースを空けることをお勧めします。
| node.val = preorder[preorder_index] | ||
| preorder_index += 1 | ||
| inorder_index = inorder_val_to_index[node.val] | ||
| if inorder_index + 1 < end: |
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.
最初right側から書いていることを不思議に思いましたが、leftの方をstackで末尾に入れないといけないからということですね。コメントがあるとスムーズに読めると思いました。
| - preorder_indexをクラス変数で保持する方法 | ||
| - nonlocalを使う方法 | ||
|
|
||
| あたりが考えられるが、安全なのは上二つか |
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.
queueとして処理する方法 と nonlocalを使う方法は実質同じかなと思ったのですが、安全かに差があると思われた理由を教えていただきたいです。nonlocal と書いてある方が書き換えていることが明確であるという気もします。
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.
複数回呼べるか、スレッド並列か、などの話かと思います。
nonlocal でも二重の関数にしてあげればこの問題はないですね。
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.
その観点まで考えられていませんでした。コメントありがとうございます。
| node = TreeNode(preorder[0]) | ||
| node_index = inorder.index(preorder[0]) | ||
| left_inorder = inorder[:node_index] | ||
| right_inorder = inorder[node_index+1:] |
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.
Step1のコードに細かいことを言って恐縮なんですが、自分ならright_inorderはrightの話なので後(L26)に持ってきますね
| left_inorder = inorder[:node_index] | ||
| right_inorder = inorder[node_index+1:] | ||
| left_inorder_set = set(left_inorder) | ||
| left_preorder = [val for val in preorder if val in left_inorder_set] |
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.
あと、やや冗長なのでleft_preorder = [val for val in preorder if val in set(left_inorder)] とすると思います
| ## Step 3. Final Solution | ||
|
|
||
| - 再帰で3回、iterativeで1回 | ||
| - stackの名前が長すぎるのが少し気になる |
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://docs.python.org/3/library/dataclasses.html
dataclass使うと、中身の定義をdataclassの方に書けるので簡潔になりますね
|
|
||
| ## Step 2. Alternatives | ||
|
|
||
| - preorderをqueueにして処理していけばsetを作ったり探索する必要がなくなるとのこと |
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.
この解法、queueの処理される順番が追いにくく個人的にはあまり好きではないです
|
|
||
| - あまり上手く出来る方法が思いつかない | ||
| - どちらから処理したら良いのか | ||
| - そもそもpreorderとinorderがどんなものかよく知らなかった |
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.
今回の問題に関係はないですが、postorderも一緒に覚えて方がいいと思います。
| inorder_index = inorder_val_to_index[node.val] | ||
| if inorder_index + 1 < end: | ||
| node.right = TreeNode() | ||
| nodes_preorder_indices_and_ranges.append((node.right, \ |
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.
Do not use a backslash for explicit line continuation.
というのもあるようです。
https://google.github.io/styleguide/pyguide.html#32-line-length
| ```python | ||
| class Solution: | ||
| def buildTree(self, preorder: List[int], inorder: List[int]) -> Optional[TreeNode]: | ||
| preorder_queue = deque(preorder) |
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.
preorder の添字を0から1づつ増やしていくことでも実現できますね。
問題文:https://leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/description/