Skip to content

111. Minimum Depth of Binary Tree#27

Open
Yuto729 wants to merge 1 commit intomainfrom
minimum-depth-of-binary-tree
Open

111. Minimum Depth of Binary Tree#27
Yuto729 wants to merge 1 commit intomainfrom
minimum-depth-of-binary-tree

Conversation

@Yuto729
Copy link
Copy Markdown
Owner

@Yuto729 Yuto729 commented Dec 30, 2025

解く問題

Minimum Depth of Binary Tree

次に解く問題

Merge Two Binary Trees

@Yuto729 Yuto729 changed the title Minimum Depth Of Binary Tree 111. Minimum Depth Of Binary Tree Dec 30, 2025
@Yuto729 Yuto729 changed the title 111. Minimum Depth Of Binary Tree 111. Minimum Depth of Binary Tree Dec 30, 2025
Repository owner deleted a comment from github-actions bot Jan 12, 2026
while frontiers:
next_frontiers = []
for node in frontiers:
if node.left is None and node.right is None:
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

過去にいただいたレビューコメントの受け売りなのですが、つまり何を判定しているのかをbooleanやinner functionの名前で表すと分かりやすいです。

Suggested change
if node.left is None and node.right is None:
is_leaf = node.left is None and node.right is None
if is_leaf:

if root is None:
return 0

node = root
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

node に置き直す意図が見えませんでした。

  • 置かない。root のまま書いちゃう
  • node を取るヘルパー関数を書いて、return helper(root) する
  • どうしても置くなら、None チェックの前にする

のいずれかが良さそうに思いました(自分ならこうするかなーという優先順)。

if root is None:
return 0

node = root
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

自分も下でmamoさんがおっしゃっているように、rootをnodeで置き換えずにそのまま使って書いたものが一番シンプルでわかりやすいかなと思いました。
個人的には下のコードよりもこちらの方が好きです。

## Step3
再帰で練習
```py
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.

全くもって自明ではある(し、自分もやっていないので人のこと言えない)んですが、TreeNodeクラスを定義しておくと、可読性は上がりそうですね。

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