-
Notifications
You must be signed in to change notification settings - Fork 0
Create 0105-construct-binary-tree-from-preorder-and-inorder-traversal.md #34
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?
Create 0105-construct-binary-tree-from-preorder-and-inorder-traversal.md #34
Conversation
| def buildTree(self, preorder: List[int], inorder: List[int]) -> Optional[TreeNode]: | ||
| inorder_value_to_index = {} | ||
| for i, value in enumerate(inorder): | ||
| inorder_value_to_index[value] = i |
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.
{value: index for index, value in enumerate(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.
ありがとうございます。
内包表記ですね。
prosはコンパクトなこと、consは若干目が左右に揺れること、でしょうか。自分は使わないほうが好みでした。
| preorder_index += 1 | ||
|
|
||
| inorder_index = inorder_value_to_index[node.val] | ||
| subtrees.append((inorder_index + 1, inorder_last, node, False)) |
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.
inorderのindexで入れるかどうかの判断はこの段階でしたいかもしれません。subtreesに入る順番がpreorderの順番と一致するというのはロジックの重要な部分だと思うので、queueに入ったにも関わらずcontinueで無視されるものがあると一瞬想像してたものと違ってイメージしにくくなりました。
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.
ありがとうございます。
同意です。
同じロジックを2回書きたくないなと思いpop時にvalidateするようにしましたが、ご指摘のとおりだと思います。やはりNone判定はpush時にしたほうが大体の場合わかりやすいですね。
| inorder_value_to_index[value] = i | ||
|
|
||
| dummy = TreeNode() | ||
| subtrees = deque() # first_index, last_index, parent, is_left |
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としてしか使わないのでただのリストでもいいかもと思いました。
| subtrees.append((0, len(inorder) - 1, dummy, True)) | ||
| preorder_index = 0 | ||
| while subtrees: | ||
| inorder_first, inorder_last, parent, is_left = subtrees.pop() |
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.
4つも引数があると関数の方がいいのではという気がします。ただ、どうせそうするならleft, rightの順番で関数を呼び出してpreorderの順番にする方法のがいいかもしれません。
| # go left | ||
| stack[-1].left = node | ||
| stack.append(node) | ||
| else: |
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.
ここで continue したほうが、ネストが浅くなり、読みやすくなると思いました。
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.
ありがとうございます。
たしかに、同意です。
| inorder_value_to_index[value] = i | ||
|
|
||
| def build_subtree(pre_first, pre_last, in_first, in_last): | ||
| if pre_first > pre_last: |
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.
自分なら
if pre_first == pre_last:
return TreeNode(val=root_value)と書きますが、好みの問題だと思います。 preorder が空の場合に動かなくなりますが、 buildTree() のほうで弾くと思います。
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.
ありがとうございます。
たしかに、length = 1は必ず通るので、そうすれば再帰の回数を概ね半減できそうで嬉しいですね。
validateの位置も同意です。
この問題:https://leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/
次の問題:https://leetcode.com/problems/paint-fence/