Skip to content

617 merge two binary trees#23

Open
MA-yo-TA wants to merge 4 commits into
mainfrom
617-Merge-Two-Binary-Trees
Open

617 merge two binary trees#23
MA-yo-TA wants to merge 4 commits into
mainfrom
617-Merge-Two-Binary-Trees

Conversation

@MA-yo-TA

@MA-yo-TA MA-yo-TA commented Jul 7, 2026

Copy link
Copy Markdown
Owner

@miyataka miyataka left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

全体的に綺麗でとても読みやすいです.


## step1(まず通す)

再帰で書くのがぱっと見わかりやすそうだったのでまずはそれで書く。再帰でスタックする数も高々木の高さなので問題ないはず。

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

再帰でスタックする数も高々木の高さなので問題ないはず。

これくらい書いておくと,わかっている感は伝わっていいですね.

pythonの再帰の上限について簡単に調べてみたのですが,default recursion limitは1000らしいです.(詳しくないので頓珍漢なこと言ってたらすみません)
https://peps.python.org/pep-0651/
なので,0 <= N <= 2000 である今回の問題では,一概に問題ないとは言えないかもしれません.

自分のローカルで試したのが以下

➜  ~ python
Python 3.14.5 (main, May 10 2026, 10:21:34) [Clang 21.0.0 (clang-2100.0.123.102)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.getrecursionlimit()
1000
>>>

leetcodeで同様にすると550000 とでました.引き上げられているのかもしれませんね.

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.

ありがとうございます。デフォルトのリミットは 1000 ですね。Python で再起が好まれない理由の一つだと思います。

与えられた木が一本道で 2000 ノードあるみたいな状態だとスタックに積まれる数も2000になるのでデフォルトだと溢れる可能性ありますね。見積もり不正確でした。ご指摘ありがとうございます。

self.right = right


class Solution:

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

再帰は明示的なstackを用いてiterativeに書き直せるので、それも書いておくといいと思います。
現実的にもデータをスタック領域から避けstack overflowを防止できるという点で重要です。

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.

ありがとうございます。書いてみます。


## step1(まず通す)

再帰で書くのがぱっと見わかりやすそうだったのでまずはそれで書く。再帰でスタックする数も高々木の高さなので問題ないはず。

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

再帰でスタックする数

日本語として不自然に感じました。スタックは stack と stuck の英単語があり、どちらもソフトウェアエンジニアリングの文脈で使われると思います。スタックすると日本語で書いた場合は、後者を指すことが多いように思います。後者は、止まる、行き詰まるという意味です。
再帰呼び出しの回数、もしくは、再帰でスタックメモリに積まれるスタックフレームの数、と書くと、正確だと思います。

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.

4 participants