112. Path Sum#24
Open
miyataka wants to merge 1 commit into
Open
Conversation
h-masder
reviewed
Jul 7, 2026
| left := hasPathSumFunc(root.Left, targetSum, pathSum) | ||
| right := hasPathSumFunc(root.Right, targetSum, pathSum) | ||
| return left || right | ||
| } |
There was a problem hiding this comment.
conditional ORが短絡評価をしてくれるので、それを使うことで、(全部見ることなく)発見したタイミングでreturn できます。
https://go.dev/ref/spec?utm_source=chatgpt.com#Logical_operators
func hasPathSumFunc(root *TreeNode, targetSum, pathSum int) bool {
if root == nil {
return false
}
// 停止条件
if root.Left == nil && root.Right == nil {
return root.Val + pathSum == targetSum
}
pathSum += root.Val
return hasPathSumFunc(root.Left, targetSum, pathSum) || hasPathSumFunc(root.Right, targetSum, pathSum)
}
Owner
Author
There was a problem hiding this comment.
たしかに.自分のコードだと短絡評価を活用できていないですね.
leetcodeの判定が0msだったので,ちょっと満足してしまっていたかもしれないですね.
コメントありがとうございます
h-masder
reviewed
Jul 7, 2026
| toVisit = append(toVisit, nodeAndPathSum{node: node.Right, pathSum: nextPathSum}) | ||
| } | ||
| return found | ||
| } |
There was a problem hiding this comment.
関数名がhasPathSumなので、true/falseを返していいのかなと思いました。
また、breakせずともその場でreturnしてもいいのかなと思いました。
for len(toVisit) > 0 {
v := toVisit[len(toVisit)-1]
toVisit = toVisit[:len(toVisit)-1]
node, pathSum := v.node, v.pathSum
if node == nil {
continue
}
nextPathSum := pathSum + node.Val
if node.Left == nil && node.Right == nil {
if nextPathSum == targetSum {
return true
}
}
toVisit = append(toVisit, nodeAndPathSum{node: node.Left, pathSum: nextPathSum})
toVisit = append(toVisit, nodeAndPathSum{node: node.Right, pathSum: nextPathSum})
}
return false
}
Owner
Author
There was a problem hiding this comment.
関数名がhasPathSumなので、true/falseを返していいのかなと思いました。
これはそれもアリだなと思います.
書いた時にはreturnする場所はあまり増やしたくないなと感じてました.
一方で普段early returnも有用だと考えてるので,なんか個人的に使い分けをもうちょい明確にしたい部分だなと気づきました.
コメントありがとうございます
h-masder
reviewed
Jul 7, 2026
| return found | ||
| } | ||
| ``` | ||
| - これを3回繰り返した |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
https://leetcode.com/problems/path-sum/