We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
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
112. 路径总和
val1 + val2 + val3 = sum
val3 = sum - val1 - val2
val3
sum - val1 - val2
/** * @param {TreeNode} root * @param {number} targetSum * @return {boolean} */ var hasPathSum = function(root, targetSum) { // 如果二叉树为空 if (!root) return false // 如果当前节点没有子节点,它就是叶子节点,只要判断root.val === targetSum if (!root.left && !root.right) return root.val === targetSum // 每一层节点都将targetSum减去root.val,最后一层只要对比叶子节点的值是否等于targetSum // 最后将结果逐层向上返回 return hasPathSum(root.left, targetSum - root.val) || hasPathSum(root.right, targetSum - root.val) };
The text was updated successfully, but these errors were encountered:
No branches or pull requests
原题链接:
112. 路径总和
解题思路:
val1 + val2 + val3 = sum
val3 = sum - val1 - val2
val3
,是否和最后sum - val1 - val2
相等即可The text was updated successfully, but these errors were encountered: