0
点赞
收藏
分享

微信扫一扫

【代码大模型的隐私安全】Unveiling Memorization in Code Models论文阅读

m逆光生长 2024-11-21 阅读 24

使用递归算法

class Solution {
    public boolean hasPathSum(TreeNode root, int targetSum) {
        // 如果节点为空,返回false
        if (root == null) {
            return false;
        }

        // 如果是叶子节点,检查路径和是否等于目标值
        if (root.left == null && root.right == null) {
            return root.val == targetSum;
        }

        // 递归检查左子树和右子树是否存在满足条件的路径
        int newTargetSum = targetSum - root.val;
        return hasPathSum(root.left, newTargetSum) || hasPathSum(root.right, newTargetSum);
    }
}
举报

相关推荐

0 条评论