Skip to content
Open
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
87 changes: 87 additions & 0 deletions md/2-10_MaximumDepthofBinaryTree.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
# 2-10_MaximumDepthofBinaryTree
Author: WaveAlchemist
Given the root of a binary tree, return its maximum depth.

A binary tree's maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.

# 1st-2nd
Diameter of Binary Treeで実装した再帰を思い出して解答したが
return 0のところを1としてしまい不正解になってしまった
後でDiameter of Binary Treeを見て修正した

``` Python
class Solution:
def maxDepth(self, root: Optional[TreeNode]) -> int:
if not root:
return 0
left_depth = self.maxDepth(root.left)
right_depth = self.maxDepth(root.right)
depth = max(left_depth, right_depth) + 1
return depth
```
# 3rd
discord上のほかの人が解答されたコードを見て自分で再構築
- https://github.com/fhiyo/leetcode/pull/23/files
- 幅優先探索  BFS
- 深さ優先探索 DFS
- https://discordapp.com/channels/1084280443945353267/1227073733844406343/1236695050902048899
-

``` Python
class Solution:
def maxDepth(self, root: Optional[TreeNode]) -> int:
def append_node(nodes, node):
if not 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.

None かどうかを調べるとき、 if node is None: と明示的に書くことを推奨するスタイルガイドがあります。

https://google.github.io/styleguide/pyguide.html#2144-decision

Always use if foo is None: (or is not None) to check for a None value. E.g., when testing whether a variable or argument that defaults to None was set to some other value. The other value might be a value that’s false in a boolean context!

return
nodes.append(node)
Comment on lines +33 to +36
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

関数内関数でも切れ目 (36行目と37行目の間) は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.

ありがとうございます.ついついやってしまうので,気を付けます

if not root:
return 0
depth = 0
nodes = [root]
while nodes:
next_depth_nodes = []
for node in nodes:
append_node(next_depth_nodes, node.left)
append_node(next_depth_nodes, node.right)
nodes = next_depth_nodes
depth += 1
return depth
```

``` Python
class Solution:
def maxDepth(self, root: Optional[TreeNode]) -> int:
if not root:
return 0
max_depth = 0
nodes_with_depth = [(root, 1)]
while nodes_with_depth:
node, depth = nodes_with_depth.pop()
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 してから、not node を確認する手もあるでしょう。その場合は、先頭の if not root もいらないかもしれません。

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.

node, depth = nodes_with_depth.pop()の下に

if not node:
   return 0

を追加で行けますね.
代わりに先頭のif not rootは無くてOKでした.

max_depth = max(depth, max_depth)
if node.left:
nodes_with_depth.append((node.left, depth + 1))
if node.right:
nodes_with_depth.append((node.right, depth + 1))
return max_depth
```

# 4th
odaさんのコメントをもとに変更
pop してから、not node を確認する手もあるでしょう。その場合は、先頭の if not root もいらないかもしれません。

``` Python
class Solution:
def maxDepth(self, root: Optional[TreeNode]) -> int:
max_depth = 0
nodes_with_depth = [(root, 1)]
while nodes_with_depth:
node, depth = nodes_with_depth.pop()
if not node:
return 0
max_depth = max(depth, max_depth)
if node.left:
nodes_with_depth.append((node.left, depth + 1))
if node.right:
nodes_with_depth.append((node.right, depth + 1))
return max_depth
```