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

阅读 26

2024-11-21

使用递归算法

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)

0 0 举报