Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# BFS
class Solution:
def minDepth(self, root: Optional[TreeNode]) -> int:
if not root:
return 0
node_queue = deque([(root, 0)])
while node_queue:
node, depth = node_queue.popleft()
if not node.left and not node.right:
break
if node.left:
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.

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

return min_node_num


# 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

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にしていました。

right_depth = float(inf)
if node.left:
left_depth = search_min_depth(node.left) + 1
if node.right:
right_depth = search_min_depth(node.right) + 1
min_depth = min(left_depth, right_depth)
return min_depth

if not root:
return 0
min_node_num = search_min_depth(root) + 1
return min_node_num


# DFSをstackで実装
class Solution:
def minDepth(self, root: Optional[TreeNode]) -> int:
if not root:
return 0
node_depth_stack = [(root, 0)]
min_depth = float(inf)
while node_depth_stack:
node, depth = node_depth_stack.pop()
if not node.left and not node.right:
min_depth = min(min_depth, depth)
continue
if node.right:
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 でよいと思います。

return min_node_num
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# BFS
class Solution:
def minDepth(self, root: Optional[TreeNode]) -> int:
if not root:
return 0
node_depth_queue = deque([(root, 0)])
while node_depth_queue:
node, depth = node_depth_queue.popleft()
if not node.left and not node.right:
return depth + 1
if node.left:
node_depth_queue.append((node.left, depth + 1))
if node.right:
node_depth_queue.append((node.right, depth + 1))


# DFS
# left_depthとright_depthをmin_depthに統合
# min_depthの初期値をintの最大値に変更
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


# DFSをstackで実装
# min_depthの初期値をintの最大値に変更
class Solution:
def minDepth(self, root: Optional[TreeNode]) -> int:
if not root:
return 0
node_depth_stack = [(root, 0)]
min_depth = sys.maxsize
while node_depth_stack:
node, depth = node_depth_stack.pop()
if not node.left and not node.right:
min_depth = min(min_depth, depth)
continue
if node.right:
node_depth_stack.append((node.right, depth + 1))
if node.left:
node_depth_stack.append((node.left, depth + 1))
return min_depth + 1
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@

class Solution:
def minDepth(self, root: Optional[TreeNode]) -> int:
if not root:
return 0
node_depth_queue = deque([(root, 0)])
while node_depth_queue:
node, depth = node_depth_queue.popleft()
if not node.left and not node.right:
break
if node.left:
node_depth_queue.append((node.left, depth + 1))
if node.right:
node_depth_queue.append((node.right, depth + 1))
min_node_num = depth + 1
return min_node_num