-
Notifications
You must be signed in to change notification settings - Fork 0
112 path sum #25
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
Open
kitano-kazuki
wants to merge
6
commits into
main
Choose a base branch
from
112-path-sum
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
112 path sum #25
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,267 @@ | ||
| # Step1 | ||
|
|
||
| ## アプローチ | ||
|
|
||
| * 今まで見た値と自分が見るべきノードを渡される | ||
| * 自分が見るノードが葉だったら, 足したものがtargetになるかかくにん | ||
| * そうじゃなかったら足したものと次見るノードを引き継ぐ | ||
| * 関数による再帰呼び出しでも, stackを使ったDFSでもいけそう | ||
| * BFSでもできそう | ||
| * 関数呼び出しの木の深さ分行われるから, 最悪の場合で5000 | ||
|
|
||
| ## Code1-1 (Recursion) | ||
|
|
||
| ```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: TreeNode | None, targetSum: int) -> bool: | ||
| if root is None: | ||
| return False | ||
| if root.left is None and root.right is None: | ||
| return root.val == targetSum | ||
| next_targetSum = targetSum - root.val | ||
| return self.hasPathSum(root.left, next_targetSum) or self.hasPathSum(root.right, next_targetSum) | ||
|
|
||
| ``` | ||
|
|
||
| ## Code1-2 (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: TreeNode | None, targetSum: int) -> bool: | ||
| if root is None: | ||
| return False | ||
| frontier = [(root, targetSum)] | ||
| while frontier: | ||
| node, target = frontier.pop() | ||
| if node.left is None and node.right is None: | ||
| if node.val == target: | ||
| return True | ||
| continue | ||
| if node.left is not None: | ||
| frontier.append((node.left, target - node.val)) | ||
| if node.right is not None: | ||
| frontier.append((node.right, target - node.val)) | ||
| return False | ||
|
|
||
|
|
||
|
|
||
| ``` | ||
|
|
||
| ## Code1-3 (BFS) | ||
|
|
||
| ```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: TreeNode | None, targetSum: int) -> bool: | ||
| if root is None: | ||
| return False | ||
|
|
||
| frontier = [(root, targetSum)] | ||
| while frontier: | ||
| next_frontier = [] | ||
| while frontier: | ||
| node, target = frontier.pop() | ||
| if node.left is None and node.right is None: | ||
| if node.val == target: | ||
| return True | ||
| continue | ||
| if node.left is not None: | ||
| next_frontier.append((node.left, target - node.val)) | ||
| if node.right is not None: | ||
| next_frontier.append((node.right, target - node.val)) | ||
| frontier = next_frontier | ||
|
|
||
| return False | ||
|
|
||
|
|
||
| ``` | ||
|
|
||
| # Step2 | ||
|
|
||
| ## Code2-1 (Recursion) | ||
|
|
||
| ```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: TreeNode | None, targetSum: int) -> bool: | ||
| if root is None: | ||
| return False | ||
| is_leaf = root.left is None and root.right is None | ||
| if is_leaf and root.val == targetSum: | ||
| return True | ||
| next_targetSum = targetSum - root.val | ||
| return self.hasPathSum(root.left, next_targetSum) or self.hasPathSum(root.right, next_targetSum) | ||
|
|
||
| ``` | ||
|
|
||
| ## Code2-2 (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: TreeNode | None, targetSum: int) -> bool: | ||
| if root is None: | ||
| return False | ||
| node_to_visit = [(root, 0)] | ||
| while node_to_visit: | ||
| node, pathsum_before_this_node = node_to_visit.pop() | ||
| is_leaf = node.left is None and node.right is None | ||
| if is_leaf and pathsum_before_this_node + node.val == targetSum: | ||
| return True | ||
| if node.left is not None: | ||
| node_to_visit.append((node.left, pathsum_before_this_node + node.val)) | ||
| if node.right is not None: | ||
| node_to_visit.append((node.right, pathsum_before_this_node + node.val)) | ||
| return False | ||
|
|
||
| ``` | ||
|
|
||
| ## Code2-3 (BFS) | ||
|
|
||
| ```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: TreeNode | None, targetSum: int) -> bool: | ||
| if root is None: | ||
| return False | ||
|
|
||
| frontier = [(root, 0)] | ||
| while frontier: | ||
| next_frontier = [] | ||
| while frontier: | ||
| node, pathsum_before_this_node = frontier.pop() | ||
| is_leaf = node.left is None and node.right is None | ||
| if is_leaf and pathsum_before_this_node + node.val == targetSum: | ||
| return True | ||
| if node.left is not None: | ||
| next_frontier.append((node.left, pathsum_before_this_node + node.val)) | ||
| if node.right is not None: | ||
| next_frontier.append((node.right, pathsum_before_this_node + node.val)) | ||
| frontier = next_frontier | ||
|
|
||
| return False | ||
|
|
||
| ``` | ||
|
|
||
| # Step3 | ||
|
|
||
| ## Code3-1 (Recursion) | ||
|
|
||
| ```python | ||
| # 1st: 2:24 | ||
| # 2nd: 0:51 | ||
| # 3rd: 0:53 | ||
|
|
||
|
|
||
| # 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: TreeNode | None, targetSum: int) -> bool: | ||
| if root is None: | ||
| return False | ||
| is_leaf = root.left is None and root.right is None | ||
| if is_leaf and root.val == targetSum: | ||
| return True | ||
| return self.hasPathSum(root.left, targetSum - root.val) or self.hasPathSum(root.right, targetSum - root.val) | ||
| ``` | ||
|
|
||
| ## Code3-2 (DFS) | ||
|
|
||
| ```python | ||
| # 1st: 3:27 | ||
| # 2nd: 1:13 | ||
| # 3rd: 1:17 | ||
|
|
||
|
|
||
| # 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: TreeNode | None, targetSum: int) -> bool: | ||
| if root is None: | ||
| return False | ||
| node_to_visit = [(root, targetSum)] | ||
| while node_to_visit: | ||
| node, target_sum_at_this_node = node_to_visit.pop() | ||
| is_leaf = node.left is None and node.right is None | ||
| if is_leaf and node.val == target_sum_at_this_node: | ||
| return True | ||
| if node.left is not None: | ||
| node_to_visit.append((node.left, target_sum_at_this_node - node.val)) | ||
| if node.right is not None: | ||
| node_to_visit.append((node.right, target_sum_at_this_node - node.val)) | ||
| return False | ||
| ``` | ||
|
|
||
| ## Code3-3 (BFS) | ||
|
|
||
| ```python | ||
| # 1st: 1:54 | ||
| # 2nd: 1:39 | ||
| # 3rd: 1:26 | ||
|
|
||
|
|
||
| # 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: TreeNode | None, targetSum: int) -> bool: | ||
| if root is None: | ||
| return False | ||
| frontier = [(root, targetSum)] | ||
| while frontier: | ||
| next_level_frontier = [] | ||
| while frontier: | ||
| node, target_sum_at_this_node = frontier.pop() | ||
| is_leaf = node.left is None and node.right is None | ||
| if is_leaf and node.val == target_sum_at_this_node: | ||
| return True | ||
| if node.left is not None: | ||
| next_level_frontier.append((node.left, target_sum_at_this_node - node.val)) | ||
| if node.right is not None: | ||
| next_level_frontier.append((node.right, target_sum_at_this_node - node.val)) | ||
| frontier = next_level_frontier | ||
| return False | ||
| ``` | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| # 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: TreeNode | None, targetSum: int) -> bool: | ||
| if root is None: | ||
| return False | ||
| if root.left is None and root.right is None: | ||
| return root.val == targetSum | ||
| next_targetSum = targetSum - root.val | ||
| return self.hasPathSum(root.left, next_targetSum) or self.hasPathSum(root.right, next_targetSum) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 hasPathSum(self, root: TreeNode | None, targetSum: int) -> bool: | ||
| if root is None: | ||
| return False | ||
| frontier = [(root, targetSum)] | ||
| while frontier: | ||
| node, target = frontier.pop() | ||
| if node.left is None and node.right is None: | ||
| if node.val == target: | ||
| return True | ||
| continue | ||
| if node.left is not None: | ||
| frontier.append((node.left, target - node.val)) | ||
| if node.right is not None: | ||
| frontier.append((node.right, target - node.val)) | ||
| return False | ||
|
|
||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| # 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: TreeNode | None, targetSum: int) -> bool: | ||
| if root is None: | ||
| return False | ||
|
|
||
| frontier = [(root, targetSum)] | ||
| while frontier: | ||
| next_frontier = [] | ||
| while frontier: | ||
| node, target = frontier.pop() | ||
| if node.left is None and node.right is None: | ||
| if node.val == target: | ||
| return True | ||
| continue | ||
| if node.left is not None: | ||
| next_frontier.append((node.left, target - node.val)) | ||
| if node.right is not None: | ||
| next_frontier.append((node.right, target - node.val)) | ||
| frontier = next_frontier | ||
|
|
||
| return False |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| # 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: TreeNode | None, targetSum: int) -> bool: | ||
| if root is None: | ||
| return False | ||
| is_leaf = root.left is None and root.right is None | ||
| if is_leaf and root.val == targetSum: | ||
| return True | ||
| next_targetSum = targetSum - root.val | ||
| return self.hasPathSum(root.left, next_targetSum) or self.hasPathSum(root.right, next_targetSum) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| # 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: TreeNode | None, targetSum: int) -> bool: | ||
| if root is None: | ||
| return False | ||
| node_to_visit = [(root, 0)] | ||
| while node_to_visit: | ||
| node, pathsum_before_this_node = node_to_visit.pop() | ||
| is_leaf = node.left is None and node.right is None | ||
| if is_leaf and pathsum_before_this_node + node.val == targetSum: | ||
| return True | ||
| if node.left is not None: | ||
| node_to_visit.append((node.left, pathsum_before_this_node + node.val)) | ||
| if node.right is not None: | ||
| node_to_visit.append((node.right, pathsum_before_this_node + node.val)) | ||
| return False | ||
|
|
||
|
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
木探索は左の子からの探索が一般的ですが、stackを利用しているため、この順序を守りたい場合にはnode.rightを先にappendするのが良いと思いました