Skip to content

Create 105. Construct Binary Tree from Preorder and Inorder Traversal.md#12

Merged
Mike0121 merged 4 commits intomainfrom
105.-Construct-Binary-Tree-from-Preorder-and-Inorder-Traversal
Nov 1, 2025
Merged

Create 105. Construct Binary Tree from Preorder and Inorder Traversal.md#12
Mike0121 merged 4 commits intomainfrom
105.-Construct-Binary-Tree-from-Preorder-and-Inorder-Traversal

Conversation

@Mike0121
Copy link
Copy Markdown
Owner

@Mike0121 Mike0121 commented May 8, 2024

Comment on lines 45 to 48
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

関数内で使う変数は、関数の定義の前に書いてあった方が個人的には読みやすいです。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

ありがとうございます、ご指摘の通りですね。
今回は関数の中でnonlocalでpreorder_indexにアクセスしたり、ハッシュマップを使っているので余計そうかもしれません。

再帰のstackを利用した解解法を追記
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

right_count を定義しておきます?
そうすると、
right_count > 0
pre_end - right_count + 1
かなんかでいけます。

Copy link
Copy Markdown
Owner Author

@Mike0121 Mike0121 May 11, 2024

Choose a reason for hiding this comment

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

ありがとうございます。確かにそっちの方が読みやすいですね。
right_countを定義した解法を追記しました。

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

閉開区間で書いてもよいかと思いました

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

80行目や85行目の判定からこの条件を満たすものしかstackに積んでないことが分かるのでこの条件は不要だと思いました。

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

中で.indexで舐めてるので0(N^2)でしょうか。

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

preorder と inorder のスライスでコピーが作られるので、木が片側に偏っている場合にも O(N2) になると思います。

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

preorder と inorder のスライスでコピーが作られるので、木が片側に偏っている場合にも O(N2) になると思います。

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

要素の数が多く、どれが何を表すか分かりにくく感じました。 dataclass や namedtuple などを用いて、各要素に名前 (変数名) を付けてあげたほうが良いと思います。

@Mike0121 Mike0121 changed the title 105. Construct Binary Tree from Preorder and Inorder Traversal 105. Construct Binary Tree from Preorder and Inorder Traversal.md Nov 1, 2025
@Mike0121 Mike0121 changed the title 105. Construct Binary Tree from Preorder and Inorder Traversal.md Create 105. Construct Binary Tree from Preorder and Inorder Traversal.md Nov 1, 2025
@Mike0121 Mike0121 merged commit 7a000ed into main Nov 1, 2025
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.

6 participants