美文网首页
2021-07-20 二叉树和为某路径上的值

2021-07-20 二叉树和为某路径上的值

作者: hlchengzi | 来源:发表于2021-07-20 20:52 被阅读0次

    private ArrayList<ArrayList<Integer>> res;
    public ArrayList<ArrayList<Integer>> FindPath(TreeNode root,int target) {
    //这不就是树的
    res = new ArrayList();
    bfs(root,target,new ArrayList<Integer>());
    return res;
    }

    private void bfs(TreeNode root,int target,ArrayList<Integer> ls){
        if(null == root){
            return;
        }
        ls.add(root.val);
        if(root.val == target && null == root.left && null == root.right){
            res.add(new ArrayList<Integer>(ls));
        }
        bfs(root.left,target-root.val,ls);
        bfs(root.right,target - root.val,ls);
        ls.remove(ls.size()-1);
    }

    相关文章

      网友评论

          本文标题:2021-07-20 二叉树和为某路径上的值

          本文链接:https://www.haomeiwen.com/subject/oauhmltx.html