Skip to content

Conversation

@kazukiii
Copy link
Owner

@kazukiii kazukiii commented Jul 8, 2024

問題へのリンク
https://leetcode.com/problems/minimum-depth-of-binary-tree/description/

次に解く問題
617. Merge Two Binary Trees

README.mdへ頭の中の言語化と記録をしています。

if not node.left and not node.right:
min_depth = min(min_depth, depth)

if node.left:

Choose a reason for hiding this comment

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

わりと好みの範囲だと思いますが、自分はleftとrightの処理を逆にします。ツリーの探索は左から見るのが自然だと思うので。今のコードだと右から探索しています。

Copy link
Owner Author

Choose a reason for hiding this comment

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

ああ、たしかにそうですね。そこまで気が回っていませんでした、ありがとうございます。

# self.right = right
class Solution:
def minDepth(self, root: Optional[TreeNode]) -> int:
nodes_to_visit = deque([(root, 1)])
Copy link

Choose a reason for hiding this comment

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

好みの問題かもしれませんが、nodes_to_visitよりも、queueの中身をより反映させるために、nodes_and_depthに自分ならするかもしれません。


min_depth = math.inf
if root.left:
min_depth = min(min_depth, self.minDepth(root.left))
Copy link

Choose a reason for hiding this comment

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

見やすさのために書いているかもしれませんが、ここはmin_depth = self.minDepth(root.left)でも通りそうですね。

Copy link
Owner Author

Choose a reason for hiding this comment

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

たしかにそれでもいいですね。
自分は関数型言語でいうところのfoldのイメージで書いてました。
単位元で初期化して、二項関数で一つにまとめていくイメージです。

return depth
nodes_to_visit.append((node.left, depth + 1))
nodes_to_visit.append((node.right, depth + 1))
return 0
Copy link

Choose a reason for hiding this comment

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

rootがNoneのときにしかここに到達しない旨のコメントはあってもいいかもなと思いました。
nodeがNoneのとき、leafのとき、内部節点のときそれぞれの場合で処理を考えないとそれが分からず、分からないと0を返して本当にいいんだっけ?と不安になるからです。

Copy link
Owner Author

@kazukiii kazukiii Jul 8, 2024

Choose a reason for hiding this comment

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

ありがとうございます。おっしゃる通りだと思いました。
もしくは、先頭でrootがNoneの場合を弾いた方が読みやすいでしょうか?

class Solution:
    def minDepth(self, root: Optional[TreeNode]) -> int:
        if not root:
            return 0
        nodes_to_visit = deque([(root, 1)])
        while nodes_to_visit:
            node, depth = nodes_to_visit.popleft()
            if not node: continue
            if not node.left and not node.right:
                return depth
            nodes_to_visit.append((node.left, depth + 1))
            nodes_to_visit.append((node.right, depth + 1))
        
        raise ValueError("unreachable")

Copy link

Choose a reason for hiding this comment

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

コメントにするか先に弾くかは趣味の範囲な気が自分はしました。どちらも読みやすいと思います!

Copy link

@Yoshiki-Iwasa Yoshiki-Iwasa left a comment

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.

6 participants