Skip to content

112. Path Sum#24

Open
miyataka wants to merge 1 commit into
mainfrom
112_PathSum
Open

112. Path Sum#24
miyataka wants to merge 1 commit into
mainfrom
112_PathSum

Conversation

@miyataka

@miyataka miyataka commented Jul 6, 2026

Copy link
Copy Markdown
Owner

Comment thread 112_PathSum/solution.md
left := hasPathSumFunc(root.Left, targetSum, pathSum)
right := hasPathSumFunc(root.Right, targetSum, pathSum)
return left || right
}

@h-masder h-masder Jul 7, 2026

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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)
}

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

たしかに.自分のコードだと短絡評価を活用できていないですね.
leetcodeの判定が0msだったので,ちょっと満足してしまっていたかもしれないですね.

コメントありがとうございます

Comment thread 112_PathSum/solution.md
toVisit = append(toVisit, nodeAndPathSum{node: node.Right, pathSum: nextPathSum})
}
return found
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

関数名が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
}

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

関数名がhasPathSumなので、true/falseを返していいのかなと思いました。

これはそれもアリだなと思います.
書いた時にはreturnする場所はあまり増やしたくないなと感じてました.
一方で普段early returnも有用だと考えてるので,なんか個人的に使い分けをもうちょい明確にしたい部分だなと気づきました.

コメントありがとうございます

Comment thread 112_PathSum/solution.md
return found
}
```
- これを3回繰り返した

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

全体的に読みやすかったです。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants