题解 | #牛的奶量统计#

牛的奶量统计

https://www.nowcoder.com/practice/213c039668804add9513bbee31370248



public class Solution {
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * 
     * @param root TreeNode类 
     * @param targetSum int整型 
     * @return bool布尔型
     */
    int now=0;//定义一个全局的变量
    public boolean hasPathSum (TreeNode root, int targetSum) {
        // write code here
        if(root==null&& targetSum==0) return false;
        now=now+root.val;
        if(root.left==null&&root.right==null){
            if(now==targetSum) return true;
            else {
                now=now-root.val;//回溯
                return false;
            }
        }
        if(root.left!=null){//在左子树找
            boolean left=hasPathSum(root.left,targetSum);
            if(left) return true;
        }
        
        if(root.right!=null){//在右子树找
            boolean right=hasPathSum(root.right,targetSum);
            if(right) return true;
        }
        now=now-root.val;//都没找到要回溯
        return false;
    }
    
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                

全部评论

相关推荐

迷茫的大四🐶:自信一点,我认为你可以拿到50k,低于50k完全配不上你的能力,兄弟,不要被他们骗了,你可以的
点赞 评论 收藏
分享
06-25 21:00
门头沟学院 Java
多拆解背记一下当前的高频场景面试题,结合自己的项目经历去作答,面试通过率原来真的不会低!
牛客96559368...:小公司不就是这样的吗,面试要么是点击就送,要么就是往死里拷打,没有一个统一的标准。这个不能代表所有公司
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务