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
17 changes: 17 additions & 0 deletions arai60/phase1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# 再帰の解き方
class Solution:
def maxDepth(self, root: Optional[TreeNode]) -> int:
max_depth = 0
def traverse_nodes(node, depth):
# node: the node you concentrate on
# depth: the depth of the node you concentrate on
nonlocal max_depth
if node == None:
return
if max_depth < depth:
max_depth = depth
traverse_nodes(node.left, depth + 1)
traverse_nodes(node.right, depth + 1)

traverse_nodes(root, 1)
return max_depth
50 changes: 50 additions & 0 deletions arai60/phase2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
"""
Reference
fhiyo: https://github.com/fhiyo/leetcode/pull/23/files phase1の段階で別の関数挟まずに書き終えてて凄すぎてびっくりしてしまった。関数プログラミングっぽい書き方
Copy link

Choose a reason for hiding this comment

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

Arai60は最近一度解いている状態でやってるので、覚えているものはできるんです... (^_^;)

nittoco: https://github.com/nittoco/leetcode/pull/14/files
rossy0213: https://github.com/rossy0213/leetcode/pull/10
Mike0121: https://github.com/Mike0121/LeetCode/pull/6#discussion_r1590279702
traverse_nodesに型ヒントを追加
"""
# 簡潔
class Solution:
def maxDepth(self, root: Optional[TreeNode]) -> int:
if root is None:
return 0
return max(self.maxDepth(root.right), self.maxDepth(root.left)) + 1



# phase1の直し, 型ヒントを追加, node == Noneをnode is Noneに
class Solution:
def maxDepth(self, root: Optional[TreeNode]) -> int:
max_depth = 0
def traverse_nodes(node: Optional[TreeNode], depth: int) -> None:
# node: the node you concentrate on
# depth: the depth of the node you concentrate on
nonlocal max_depth
if node is None:
return
if max_depth < depth:
max_depth = depth
traverse_nodes(node.left, depth + 1)
traverse_nodes(node.right, depth + 1)

traverse_nodes(root, 1)
return max_depth

# 一応再帰以外でも
class Solution:
def maxDepth(self, root: Optional[TreeNode]) -> int:
max_depth = 0
stack = [(root, 1)]
while stack:
current_node, depth_so_far = stack.pop()
Copy link

Choose a reason for hiding this comment

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

depth_so_farって「これまでの深さ」みたいな意味になりますか? (英語は自信ないです)
単なるノードの深さなのでdepthで良いような気がしました。

if current_node is None:
continue
if max_depth < depth_so_far:
max_depth = depth_so_far
stack.append((current_node.right, depth_so_far + 1))
stack.append((current_node.left, depth_so_far + 1))
Comment on lines +41 to +48
Copy link

Choose a reason for hiding this comment

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

自分はそれぞれif current_node.right とif current_node,leftがNoneでないならappend、と書いてましたが、Takahashiさんのcurrent_nodeがNoneなら更新しない、という方が簡潔でいいですね


return max_depth
16 changes: 16 additions & 0 deletions arai60/phase3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class Solution:
def maxDepth(self, root: Optional[TreeNode]) -> int:
max_depth = 0
def traverse(node: Optional[TreeNode], depth: int) -> None:
# node: the node you concentrate on
# depth: the depth of the node you concentrate on
nonlocal max_depth
if node is None:
return
if max_depth < depth:
max_depth = depth
Comment on lines +10 to +11

Choose a reason for hiding this comment

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

行きがけでmax_depthを更新する場合、leafに到達したときだけ更新してもいいかもしれません。

Copy link
Owner Author

Choose a reason for hiding this comment

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

確かにそちらの方が効率がよさそうです

traverse(node.right, depth + 1)
traverse(node.left, depth + 1)

traverse(root, 1)
return max_depth