Skip to content
Open
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
30 changes: 30 additions & 0 deletions arai60/maximum-depth-of-binary-tree/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
## 考察
- 過去に解いたことあり
- 方針
- DFS
- 左部分木と右部分木のdepthのmaxを取って1を追加すれば良さそう
- 帰りがけで計算したいので再帰で書きたいところ
- 再帰の深さが最大10^4になるのが気になるが、recursionlimitが550000に設定されているので大丈夫そう
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

むしろ、私の感覚は逆で、「この環境では大丈夫」なコードは可能ならば避けたいです。いつ、自分の足を撃ち抜くか分からないからです。

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.

たしかにそうですね。ちょっと感覚をアップデートします。

- BFS
- 深さ(レベル)別に見ていけば、最後に探索するノードの深さが答え
- Step1ではDFSで実装してみる

## Step1
- 再帰DFSで実装
- time: O(n), space: O(n)

## Step2
- BFSでも実装
- キューのところをスタックにすれば非再帰DFSにもなる
- time: O(n), space: O(n)
- 他の人のPRを検索
- 行きと帰りのラベルを付けてスタックに積めば、帰りがけの計算も非再帰で表現可能
- https://discord.com/channels/1084280443945353267/1201211204547383386/1228400327800127648

## Step3
- 1回目: 28s
- 2回目: 28s
- 3回目: 26s

## Step4
- キューにdepthを入れないversionのBFSを追加
11 changes: 11 additions & 0 deletions arai60/maximum-depth-of-binary-tree/step1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def maxDepth(self, root: Optional[TreeNode]) -> int:
if not root:
return 0
return max(self.maxDepth(root.left), self.maxDepth(root.right)) + 1
19 changes: 19 additions & 0 deletions arai60/maximum-depth-of-binary-tree/step2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def maxDepth(self, root: Optional[TreeNode]) -> int:
nodes_to_visit = deque([(root, 1)])
max_depth = 0

while nodes_to_visit:
node, depth = nodes_to_visit.popleft()
if not node: continue
max_depth = max(max_depth, depth)
nodes_to_visit.append((node.left, 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.

自分ならnode.leftがNoneのときはnodes_to_visitに入れないですかね。

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

これ、良し悪しで None が入らないとすると、root が None でないことも確認する必要がありますね。

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

たしかに、そっちもなおさないとですね。

nodes_to_visit.append((node.right, depth + 1))

return max_depth
11 changes: 11 additions & 0 deletions arai60/maximum-depth-of-binary-tree/step3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def maxDepth(self, root: Optional[TreeNode]) -> int:
if not root:
return 0
return max(self.maxDepth(root.left), self.maxDepth(root.right)) + 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 = self.maxDepth(root.left)
        right_depth = self.maxDepth(root.right)
        depth = max(left_depth, right_depth) + 1
        return 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.

左の木の深さと右の木の深さを明示して書いても見やすいですね。ありがとうございます。

24 changes: 24 additions & 0 deletions arai60/maximum-depth-of-binary-tree/step4.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def maxDepth(self, root: Optional[TreeNode]) -> int:
if not root:
return 0

max_depth = 0
nodes_to_visit = deque([root])
while nodes_to_visit:
max_depth += 1
level_size = len(nodes_to_visit)
for _ in range(level_size):
node = nodes_to_visit.popleft()
if node.left:
nodes_to_visit.append(node.left)
if node.right:
nodes_to_visit.append(node.right)

return max_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.

ただの感想ですみませんが、この選択肢なかったので勉強になりました🙏

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

私もご指摘いただいたのですが,
pop後にnodeがNoneかどうかを確認する方法もありますね.
https://discord.com/channels/1084280443945353267/1218740927120674977/1259562704201318492

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.

@NobukiFukui
レビューありがとうございます。
こちらのキューにdepthを入れない方のBFSの書き方だと、nodeがNoneの場合はキューに入れてはだめな気がしています。