Skip to content

【Arai60】22問目 111_Minimum Depth of Binary Tree#22

Merged
shining-ai merged 2 commits intomainfrom
review22
Jun 30, 2024
Merged

【Arai60】22問目 111_Minimum Depth of Binary Tree#22
shining-ai merged 2 commits intomainfrom
review22

Conversation

@shining-ai
Copy link
Copy Markdown
Owner

node_queue.append((node.left, depth + 1))
if node.right:
node_queue.append((node.right, depth + 1))
min_node_num = depth + 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.

あくまで C++ をメインで使っている人間の印象なのですが、ループの中で定義した変数をループの外で参照している点に違和感を感じました。 break するのではなく、直接 return depth + 1 してしまったほうが、個人的には直感的に思いました。
また、 min_node_num というフレーズですと、 the number of nodes along the shortest path の部分が伝わらないように思いました。関数名と被ってやや微妙ですが、 min_depth でも良いように思いました。

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.

ループ内で定義した変数は、ループ内のローカル変数といったような意識で使ってみます。
意識したことなかったので、気にしてみます。

# DFS
class Solution:
def minDepth(self, root: Optional[TreeNode]) -> int:
def search_min_depth(node):
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

好みの問題なのですが、 minDepth() をそのまま使って再帰しても良いと思いました。

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.

minDepth() をそのまま使い、少しコードをすっきりさせることができました。

class Solution:
    def minDepth(self, root: Optional[TreeNode]) -> int:
        if not root:
            return 0
        if not root.left and not root.right:
            return 1
        min_depth = sys.maxsize
        if root.left:
            min_depth = min(min_depth, self.minDepth(root.left) + 1)
        if root.right:
            min_depth = min(min_depth, self.minDepth(root.right) + 1)
        return min_depth

def search_min_depth(node):
if not node.left and not node.right:
return 0
left_depth = float(inf)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

depth を求めようとしているにもかかわらず、 float を使用している点に違和感を感じました。

if not node.left and not node.right:
    return 0
elif node.left:
    return search_min_depth(node.right) + 1
elif node.right:
    return search_min_depth(node.left) + 1
else:
    min(search_min_depth(node.left), search_min_depth(node.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.

[2,null,3,null,4,null,5,null,6] のパターンで 1を出力するとWrong Answerになってしまいます。
このケースに対応に良い方法が思いつかなかったため、min_depthの初期値をmaxsizeにしていました。

node_depth_stack.append((node.right, depth + 1))
if node.left:
node_depth_stack.append((node.left, depth + 1))
min_node_num = min_depth + 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.

return min_depth + 1 でよいと思います。

Copy link
Copy Markdown

@hayashi-ay hayashi-ay left a comment

Choose a reason for hiding this comment

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

特段気になるところはなかったです。強いていうならdepthの解釈。

def search_min_depth(node):
if not node.left and not node.right:
return 0
min_depth = sys.maxsize
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

C言語でいうUINT_MAXみたいな感じなんですね。あまり見慣れないですが選択肢としては良いんじゃないかなと思います。

@shining-ai shining-ai merged commit 6df255c into main Jun 30, 2024
@shining-ai shining-ai deleted the review22 branch June 30, 2024 03:03
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.

3 participants