Skip to content

Conversation

@TORUS0818
Copy link
Owner

# self.val = val
# self.left = left
# self.right = right
class Solution:
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

これちょっと読みにくかったです。理由はっきりと分からないんですが、append_next_candidatesが何にappendするのかが全体から探さないと分からないからかもしれないと思いました。
関数に分けてるのも個人的には無い方が読みやすいかなと思いました。目の移動が多いデメリットが勝ったのかもしれないです

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

コメントありがとうございます。
append_next_candidatesは名前を横着しましたかね。。
一方で、関数化は書く側としては頭が整理できてよかったのかなと感じています(というか一気に書き下すと、うまく書けなかった気がします)

node.right = TreeNode(right_preorder[0])
node_and_order_pairs.append((node.right, right_preorder, right_inorder))

root = TreeNode(preorder[0])
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Step1では空配列のケアをしていますが、こちらと1つ下の解法ではしていないですね

return build_tree_helper(0, len(preorder))
```
思考ログ:
- preorder_indexを内部で1ずつ増やしていくのがやや分かりにくいか
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

自分もこれは分かりにくいかなと思いました。

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

このタイミングでpreorderのindexズラしていって問題ないんだっけ、と少し詰まりました。

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

自分も理解に時間がかかりました。preorderのindexのstartとendを渡してあげると良さそうです。

```
思考ログ:
- ```.index```で捜索したり、スライスしたりと効率的ではないのだけど、シンプルで分かりやすい気はする
- ```preorder```のスライスに使ってる```inorder_split_index```は不親切かも、冗長でも```num_left_subtree_node```とかに入れておいた方がいいかもしれない
Copy link

@Yoshiki-Iwasa Yoshiki-Iwasa Aug 21, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

````inorder_split_index```は不親切かも

自分もそう思いました。わざわざ感ありますが、inorderを左右に分割する関数定義してもよさそうです

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

コメントありがとうございます。

短く書けてリーズナブルな気がしたので敢えて手を加えなかったのですが、もう少し丁寧にやっても良かったですね。

return build_tree_helper(0, len(preorder))
```
思考ログ:
- preorder_indexを内部で1ずつ増やしていくのがやや分かりにくいか

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

自分も理解に時間がかかりました。preorderのindexのstartとendを渡してあげると良さそうです。

inorder_value_to_index = {value: index for index, value in enumerate(inorder)}

preorder_index = 0
def build_tree_helper(inorder_left_bound: int, inorder_right_bound: int) -> TreeNode:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

細かいですが、return type は Optional[TreeNode] ですかね。

return None

root_val = preorder[0]
inorder_split_index = inorder.index(root_val)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

この時点で変数名をleft_subtree_sizeとして良いかと思いました。
そうすると例えば下で、preorder[1:1+left_subtree_size] となり、インデックス1から長さが左部分木のサイズと等しいだけ切り出していることが一目でわかります。

val = preorder[0]
inorder_split_index = inorder.index(val)

left_tree_inorder = inorder[:inorder_split_index]
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

スライスをつくるより、インデックスの範囲を表す整数を用いたほうが、コピーが発生せず、処理が軽くなると思います。

def buildTree(self, preorder: List[int], inorder: List[int]) -> Optional[TreeNode]:
inorder_value_to_index = {value: index for index, value in enumerate(inorder)}

preorder_index = 0
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

個人的には、使った preorder_index の数を build_tree_helper が一緒に返す形にするかなあと思いました。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants