-
Notifications
You must be signed in to change notification settings - Fork 0
112. Path Sum #19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
112. Path Sum #19
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,32 @@ | ||
| # Step 1 | ||
|
|
||
| - recursionで試したが、上手くいかなかったのでまずiterativeで試す | ||
|
|
||
| ```python | ||
| # 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 hasPathSum(self, root: Optional[TreeNode], targetSum: int) -> bool: | ||
| if root is None: | ||
| return False | ||
|
|
||
| stack = [(root, root.val)] | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. こちらのコメントをご参照ください。 |
||
| while stack: | ||
| node, current_sum = stack.pop() | ||
| if node: | ||
| if node.left is None and node.right is None and current_sum == targetSum: | ||
| return True | ||
| if node.left is not None: | ||
| stack.append((node.left, current_sum + node.left.val)) | ||
| if node.right is not None: | ||
| stack.append((node.right, current_sum + node.right.val)) | ||
| return False | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ほぼ同じですが、これでもいいかなと思いました。 class Solution:
def hasPathSum(self, root: Optional[TreeNode], targetSum: int) -> bool:
if root is None:
return False
stack = [(root, targetSum)]
while stack:
node, targetSum = stack.pop()
if node:
if node.left is None and node.right is None and node.val == targetSum:
return True
if node.left is not None:
stack.append((node.left, targetSum - node.val))
if node.right is not None:
stack.append((node.right, targetSum - node.val))
return False |
||
| ``` | ||
| 時間計算量: | ||
|
|
||
| 空間計算量: | ||
| 時間計算量: O(n) | ||
|
|
||
| 空間計算量: O(n) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,60 @@ | ||
| # Step 2 | ||
|
|
||
| ## Iterative DFS | ||
|
|
||
| ```python | ||
| # 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 hasPathSum(self, root: Optional[TreeNode], targetSum: int) -> bool: | ||
| if root is None: | ||
| return False | ||
|
|
||
| nodes_and_sums = [(root, root.val)] | ||
| while nodes_and_sums: | ||
| node, current_sum = nodes_and_sums.pop() | ||
| if node: | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 必要なさそうですかね? 必要だとしても、ネストを一段減らすために、 また、Noneかどうかのチェックの方法に一貫性がないのが気になります。 |
||
| if node.left is None and node.right is None and current_sum == targetSum: | ||
| return True | ||
|
Comment on lines
+21
to
+22
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| if node.left is not None: | ||
| nodes_and_sums.append((node.left, current_sum + node.left.val)) | ||
| if node.right is not None: | ||
| nodes_and_sums.append((node.right, current_sum + node.right.val)) | ||
| return False | ||
| ``` | ||
|
|
||
| 時間計算量: O(n) | ||
|
|
||
| 空間計算量: O(n) | ||
|
|
||
| ## Recursive DFS | ||
|
|
||
| - 他の人のコードを拝見 | ||
|
|
||
| ```python | ||
| # 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 hasPathSum(self, root: Optional[TreeNode], targetSum: int) -> bool: | ||
| def dfs(node, current_sum): | ||
| if node is None: | ||
| return False | ||
|
|
||
| current_sum += node.val | ||
| if node.left is None and node.right is None: | ||
| return current_sum == targetSum | ||
| return dfs(node.left, current_sum) or dfs(node.right, current_sum) | ||
| return dfs(root, 0) | ||
| ``` | ||
|
|
||
| 時間計算量: O(n) | ||
|
|
||
| 空間計算量: O(n) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,13 @@ | ||
| # Step 3 | ||
|
|
||
| - 思いつかなかったRecursive DFSで練習 | ||
|
|
||
| ```python | ||
|
|
||
| ``` | ||
| 1回目: 分 秒 | ||
|
|
||
| 2回目: 分 秒 | ||
| 1回目: 1分 24秒 | ||
|
|
||
| 2回目: 1分 30秒 | ||
|
|
||
| 3回目: 分 秒 | ||
| 3回目: 1分 18秒 |
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
PRをマージしないにしても、テンプレフォルダーをコピーして、問題ごとにフォルダーを作ると、diffが綺麗に出て、(マージするなら)ローカルにファイルも残るので良いと思います。