题解 | #牛的奶量统计#
牛的奶量统计
https://www.nowcoder.com/practice/213c039668804add9513bbee31370248
所用语言
Java
所用知识
二叉树
解题思路
递归判断左右子树的值是否等于目标值减去根结点的值
完整代码
public boolean hasPathSum (TreeNode root, int targetSum) {
// write code here
if(root==null){
return false;
}
if(root.left==null&&root.right==null){
return root.val==targetSum;
}
return hasPathSum(root.left,targetSum-root.val) ||
hasPathSum(root.right,targetSum-root.val);
}
#牛的奶量统计#
查看1道真题和解析