Skip to content

【Arai60】21問目 104_Maximum Depth of Binary Tree#21

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

【Arai60】21問目 104_Maximum Depth of Binary Tree#21
shining-ai merged 2 commits intomainfrom
review21

Conversation

@shining-ai
Copy link
Copy Markdown
Owner

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がない時の戻り値がすぐ分かって、少し分かりやすくなると思いました。

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.

コメントしました。

def search_max_depth(node):
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カウントになってしまうということですね。

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を返しても動きますね。

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.

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

@@ -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.

好みの問題かもですが、ヘルパー関数作らなくても書けますね。

@shining-ai shining-ai merged commit e37b46c into main Jun 30, 2024
@shining-ai shining-ai deleted the review21 branch June 30, 2024 03:04
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