Skip to content

Conversation

@Satorien
Copy link
Owner

- 取りあえず左と右に分けて再帰に任せる方針で実装
- preorderの先頭が出るまでinorderで探して、出てきたら左と右で区切る
- 左は都度追加していく方が効率良さそうだが少し対称性が難あり
- left_inorderのままleft_preorderのフィルタリングをするとTLEになる
Copy link

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]:
Copy link

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:

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を使う方法

あたりが考えられるが、安全なのは上二つか

Choose a reason for hiding this comment

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

queueとして処理する方法 と nonlocalを使う方法は実質同じかなと思ったのですが、安全かに差があると思われた理由を教えていただきたいです。nonlocal と書いてある方が書き換えていることが明確であるという気もします。

Copy link

Choose a reason for hiding this comment

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

複数回呼べるか、スレッド並列か、などの話かと思います。
nonlocal でも二重の関数にしてあげればこの問題はないですね。

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:]
Copy link

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]
Copy link

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の名前が長すぎるのが少し気になる
Copy link

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を作ったり探索する必要がなくなるとのこと
Copy link

Choose a reason for hiding this comment

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

この解法、queueの処理される順番が追いにくく個人的にはあまり好きではないです


- あまり上手く出来る方法が思いつかない
- どちらから処理したら良いのか
- そもそもpreorderとinorderがどんなものかよく知らなかった

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, \

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)

Choose a reason for hiding this comment

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

preorder の添字を0から1づつ増やしていくことでも実現できますね。

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.

8 participants