-
Notifications
You must be signed in to change notification settings - Fork 0
Path sum #37
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: arai60
Are you sure you want to change the base?
Path sum #37
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 |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| class Solution: | ||
| def hasPathSum(self, root: Optional[TreeNode], targetSum: int) -> bool: | ||
| if not root: | ||
| return False | ||
|
|
||
| targetSum = targetSum - root.val # update targetSum | ||
| if targetSum == 0 and not root.right and not root.left: | ||
| return True | ||
| return self.hasPathSum(root.right, targetSum) | self.hasPathSum(root.left, targetSum) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| """ | ||
| Reference: | ||
| fhiyo: https://github.com/fhiyo/leetcode/pull/27/files | ||
| sakupan102: https://github.com/sakupan102/arai60-practice/pull/26#discussion_r1593805972 | ||
|
|
||
| ・phase1ではtargetSumを更新して0かどうかを判断していたが, 葉だったらtargetSum == root.valかどうかを返してそれ以外はrest = targetSum - root.valとして処理する方が好みだったので書き換える。 | ||
| ・|演算子よりかはor演算子の方が良いか(|はビット演算子, orの方がboolを扱う時の方では馴染みがありそう...?) | ||
|
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. 補足程度に...。
とあるので、 "and", "or", "!=" を使う方が好まれるようです
Owner
Author
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 not root: | ||
| return False | ||
|
|
||
| if not root.right and not root.left: | ||
| return targetSum == root.val | ||
| rest = targetSum - root.val # update targetSum | ||
| return self.hasPathSum(root.right, rest) or self.hasPathSum(root.left, rest) | ||
|
|
||
| class Solution: | ||
| def hasPathSum(self, root: Optional[TreeNode], targetSum: int) -> bool: | ||
| stack = [(root, targetSum)] | ||
| while stack: | ||
| node, current_sum = stack.pop() | ||
|
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. 好みかもですが、dequeにしてpopleftにした方が、BFSにするのであれば素直な処理の順番になると思いました。
Owner
Author
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. 今回はDFSのつもりで書いたのですがどうでしょうか....? 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. これはDFSです。 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. すみません、きちんとDFSになっています。 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. スタックならDFS、キューならBFSになります。 |
||
| if not node: | ||
| continue | ||
| if not node.right and not node.left and node.val == current_sum: | ||
| return True | ||
|
|
||
| rest = current_sum - node.val | ||
|
Comment on lines
+27
to
+30
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 not node.left and not node.right:
return rest == 0のほうが素直ではないでしょうか。
Owner
Author
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. その訂正の仕方だと, まだ探索しきっていない葉のうち, rest == 0となるものがあるのにrest != 0となるものが先に判定に来ると間違った回答を出してしまう気がします。rest = current_sum - node.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. この場合は、node.left, node.right がともに None なのでいいんじゃないですか。
Owner
Author
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. 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. 再帰ではなく、iterativeな解法なので、ここでreturnすると最終結果になってしまいます。 if not node.left and not node.right and rest == 0:
return TrueThere 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. おっと、とんでもない勘違いをしていましたね。なぜか再帰な気持ちになっていました。 |
||
| stack.append((node.right, rest)) | ||
| stack.append((node.left, rest)) | ||
| return False | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| class Solution: | ||
| def hasPathSum(self, root: Optional[TreeNode], targetSum: int) -> bool: | ||
| if not root: | ||
| return False | ||
| if not root.right and not root.left: | ||
| return targetSum == root.val | ||
| rest = targetSum - root.val | ||
| return self.hasPathSum(root.left, rest) or self.hasPathSum(root.right, rest) |

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.
再帰できる回数の上限を考えた上での実装判断ですかね?そうであれば良いと思います
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.
問題文にbinary tree, The number of nodes in the tree is in the range [0, 5000].とあったので木の高さは深くてもlog_2(n)ぐらいだろうと思いましたが, 片方に偏る場合もテストケースとして考えられるので再帰回数増やすかやめた方が良さそうですね...