Skip to content

104. Maximum Depth of Binary Tree#26

Open
Yuto729 wants to merge 1 commit intomainfrom
maximum-depth-of-binary-tree
Open

104. Maximum Depth of Binary Tree#26
Yuto729 wants to merge 1 commit intomainfrom
maximum-depth-of-binary-tree

Conversation

@Yuto729
Copy link
Owner

@Yuto729 Yuto729 commented Dec 30, 2025

@Yuto729 Yuto729 changed the title Maximum Depth Of Binary Tree 104. Maximum Depth Of Binary Tree Dec 30, 2025
@Yuto729 Yuto729 changed the title 104. Maximum Depth Of Binary Tree 104. Maximum Depth of Binary Tree Dec 30, 2025
Repository owner deleted a comment from github-actions bot Dec 30, 2025
Copy link

@t9a-dev t9a-dev left a comment

Choose a reason for hiding this comment

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

良いと思いました。

Comment on lines +90 to +91
max_depth = max(self.maxDepth(node.left), self.maxDepth(node.right)) + 1
return max_depth
Copy link

Choose a reason for hiding this comment

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

全体的に読みやすかったです。

ここは、return max(self.maxDepth(root.left), self.maxDepth(root.right)) + 1と1行にまとめてもいいかなと思いました。

return 0

max_depth = 1
stack = [(root, 1)]
Copy link

Choose a reason for hiding this comment

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

(Step 2で言及されていますが)一時スタックであっても中身のわかる変数名が良いと感じました.

Comment on lines +25 to +31
while stack:
node, depth = stack.pop()
if node.left is not None:
stack.append((node.left, depth + 1))
if node.right is not None:
stack.append((node.right, depth + 1))
max_depth = max(max_depth, depth)
Copy link

Choose a reason for hiding this comment

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

こちらのコードでは先にnullチェックを行っていますが,とりあえずスタックに追加してからnullチェックをする手もあります:

        while stack:
            node, depth = stack.pop()
            if not node:
                continue
            stack.append((node.left, depth + 1))
            stack.append((node.right, depth + 1))
            max_depth = max(max_depth, depth)

どちらが良いかは問題次第と思いますが,今回はどちらでも大差ないでしょう.

return 0

max_depth = 1
frontiers = deque()
Copy link

Choose a reason for hiding this comment

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

nit: frontierと単数にした方が英語的には正しそうです(cf

Comment on lines +64 to +67
frontiers = [root]
depth = 0
while frontiers:
depth += 1
Copy link

Choose a reason for hiding this comment

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

ここちょっと難しいですね。[root] の中身があることによって、root の depth が1だと分かるわけですね。

とはいえ、あまり解決策があるわけではないですが。Step3 のほうが明瞭ではあると思います。

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.

5 participants