Skip to content

111. Minimum Depth of Binary Tree#22

Open
MA-yo-TA wants to merge 1 commit into
mainfrom
111-Minimum-Depth-of-Binary-Tree
Open

111. Minimum Depth of Binary Tree#22
MA-yo-TA wants to merge 1 commit into
mainfrom
111-Minimum-Depth-of-Binary-Tree

Conversation

@MA-yo-TA

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

Copy link
Copy Markdown
Owner


みたいな時にはだいぶ計算量が違う。

逆に、root から唯一の葉まで一本道みたいなときは見るノード数が同じになる。

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.

どの葉も同じ深さにあるようなときも見るノード数は同じですね。


違う点は、一番深いところは全ノード見ないとがわからないが、一番浅いところは同じ高さごとに見ていって、最初に見つかった葉のところなのでそれ以降を見なくて済む。

再帰で自然に書くと全部見ることになる。

@h-masder h-masder Jul 2, 2026

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

一応ですが、再帰というより、深さ優先探索を再帰でやると、ですね。

(私はこういう書き方はあまりしませんが、) 幅優先探索を再帰で書くことですべてを探索しないようにすることはできます。

class Solution:
    def minDepth(self, root: Optional[TreeNode]) -> int:
        if root is None:
            return 0

        def min_depth_helper(nodes: List[TreeNode], depth) -> int:
            next_nodes = []
            for node in nodes:
                if node.left is None and node.right is None:
                    return depth + 1
                if node.left is not None:
                    next_nodes.append(node.left)
                if node.right is not None:
                    next_nodes.append(node.right)

            return min_depth_helper(next_nodes, depth + 1)

        return min_depth_helper([root], 0)

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.

ありがとうございます。そうですね、自分の中で明示的な区別ができてなくて自然にという言葉で流してしまってましたが、本来は再帰で書くことと深さ優先探索をすることは別のことですよね。
指摘してくださってありがとうございます。好みでなくてもそういう書き方ができるということ自体は考慮に入れた上で選べるようにします。

return self.minDepth(root.right) + 1
if root.right is None:
return self.minDepth(root.left) + 1
return min(self.minDepth(root.left), self.minDepth(root.right)) + 1

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

改行があったほうが見やすいかなと思いました。

    def minDepth(self, root: Optional[TreeNode]) -> int:
        if root is None:
            return 0

        if root.left is None and root.right is None:
            return 1

        if root.left is None:
            return self.minDepth(root.right) + 1

        if root.right is None:
            return self.minDepth(root.left) + 1

        return min(self.minDepth(root.left), self.minDepth(root.right)) + 1

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.

ありがとうございます。今回は確かにそうかもしれませんね。


## step3(10分以内にさっとかける \* 3回)

結局ループでは最初から次のレイヤーを探しに行きたい気がして諸々の更新処理は全部最後にした。仕事の引き継ぎだとすると、朝来て「細かいところは昨日のやつ全部やっといてくれよ」と思いそう。

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

他の人のコードを読んでいてとてもいいなと思いました。

私は、

https://docs.google.com/document/d/11HV35ADPo9QxJOpJQ24FcZvtvioli770WWdZZDaLOfg/edit?tab=t.0#heading=h.ydq9ulueycy9

に書いてある

自分が読むときの許容度は広く、書く時は自分なりの統一した狭い幅に収めるという非対称性も必要になるでしょう。スタイルガイドを強制することは普通はできないので、かなり広く読めるようにして、ただし、自分のこだわりはもっておくということが必要になります。

を参考にして読んでいます。また、許容度としては、やりたいこととコードの表現が一致しているかどうかを見ています。

@h-masder h-masder Jul 2, 2026

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

https://github.com/MA-yo-TA/leetcode/pull/21/changes#r3512713879

上記のやり方も許容される範囲に入っています。

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.

ありがとうございます。いろいろな書き方を読めるようになるためにも今後も他の方のコードをいろいろ読もうと思います。

加えて、前回と今回レビューいただいて、自分の好みを持つ(知る)ためには色々考えながら複数パターンをある程度の量書くことが必要だなと感じたのでそのへんもこの練習会で意識して取り組みます。

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.

2 participants