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,47 @@
# DFS
class Solution:
def maxDepth(self, root: Optional[TreeNode]) -> int:

def search_max_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.

補助関数に分けても良いと思いますし、 maxDepth() と引数が同じため、 maxDepth() を直接再帰呼び出ししても良いと思いました。

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.

補助関数なしでできましたね。
rootがない時の戻り値がすぐ分かって、少し分かりやすくなると思いました。

if not node:
return 0
left_depth = search_max_depth(node.left) + 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.

left_depthという命名だと1足さないものな気がするんですよね。

left_depth = search_max_depth(node.left)

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.

深さだとrootが0カウントになってしまうということですね。

right_depth = search_max_depth(node.right) + 1
max_depth = max(left_depth, right_depth)
return max_depth

return search_max_depth(root)


# BFS
class Solution:
def maxDepth(self, root: Optional[TreeNode]) -> int:
if not root:
return 0
node_depth_queue = deque([(root, 1)])
max_depth = 0
while node_depth_queue:
node, depth = node_depth_queue.popleft()
if node.left:
node_depth_queue.append((node.left, depth + 1))
if node.right:
node_depth_queue.append((node.right, depth + 1))
max_depth = max(max_depth, depth)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

BFSなのでmax取らなくても良いですね。

Suggested change
max_depth = max(max_depth, depth)
max_depth = 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.

一番最後のdepthが最大値で決まってますね。
max_depth変数作らずにdepthを返しても動きますね。

return max_depth


# stackで深さ優先探索
class Solution:
def maxDepth(self, root: Optional[TreeNode]) -> int:
if not root:
return 0
stack_node_depth = [(root, 1)]
max_depth = 1
while stack_node_depth:
node, depth = stack_node_depth.pop()
if node.left:
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

好みの問題ですが、DFSの探索をするときは左側から見ていくのが自然な気がします。そうなるとstackに積む順番はnode.right, node.leftの順になります。

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.

確かに探索は左からが自然だと思いました。

stack_node_depth.append((node.left, depth + 1))
if node.right:
stack_node_depth.append((node.right, depth + 1))
max_depth = max(max_depth, depth)
return max_depth
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# DFS
# 特に変更なし
class Solution:
def maxDepth(self, root: Optional[TreeNode]) -> int:
if not root:
return 0
left_depth = self.maxDepth(root.left) + 1
right_depth = self.maxDepth(root.right) + 1
max_depth = max(left_depth, right_depth)
return max_depth


# BFS
# 特に変更なし
class Solution:
def maxDepth(self, root: Optional[TreeNode]) -> int:
if not root:
return 0
node_depth_queue = deque([(root, 1)])
while node_depth_queue:
node, depth = node_depth_queue.popleft()
if node.left:
node_depth_queue.append((node.left, depth + 1))
if node.right:
node_depth_queue.append((node.right, depth + 1))
max_depth = depth
return max_depth


# stackで深さ優先探索
# 特に変更なし
class Solution:
def maxDepth(self, root: Optional[TreeNode]) -> int:
if not root:
return 0
stack_node_depth = [(root, 1)]
max_depth = 1
while stack_node_depth:
node, depth = stack_node_depth.pop()
if node.left:
stack_node_depth.append((node.left, depth + 1))
if node.right:
stack_node_depth.append((node.right, depth + 1))
max_depth = max(max_depth, depth)
return max_depth
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class Solution:
def maxDepth(self, root: Optional[TreeNode]) -> int:
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 search_max_depth(node):
if not node:
return 0
left_depth = search_max_depth(node.left) + 1
right_depth = search_max_depth(node.right) + 1
max_depth = max(left_depth, right_depth)
return max_depth

return search_max_depth(root)