二叉树路径和为一固定值

二叉树类题目,第一反应肯定就是使用递归(dfs)。这道题也不例外。

还是先给出我的代码。主要注意数组的引用性。所以每次都要去掉尾数。

并在最后放入结果集中时,执行深copy。

import java.util.ArrayList;
/**
public class TreeNode {
    int val = 0;
    TreeNode left = null;
    TreeNode right = null;

    public TreeNode(int val) {
        this.val = val;

    }

}
*/
public class Solution {
    public ArrayList<ArrayList<Integer>> FindPath(TreeNode root,int target) {
        ArrayList<ArrayList<Integer>> result = new ArrayList<>();
        ArrayList<Integer> array = new ArrayList<>();
        find(root,target,result,array);
        
        return result;
    }
    void find(TreeNode root,int target,ArrayList<ArrayList<Integer>> result,ArrayList<Integer> array){
        if(null == root){
           return;
        }
        array.add(root.val);
        target-=root.val;
        if(0 == target && null == root.left && null == root.right){
            result.add(new ArrayList<>(array)); //直接赋值是引用赋值,指向相同对象。array最终回到顶层就只有一个10。
            return;
        }
        
        if(null !=root.left){
            find(root.left,target,result,array);
            array.remove(array.size() -1);
        }
        if(null != root.right){
            find(root.right,target,result,array);
            array.remove(array.size() -1);
        }
        
    }
}

利用类变量的简化版:

import java.util.ArrayList;
/**
public class TreeNode {
    int val = 0;
    TreeNode left = null;
    TreeNode right = null;

    public TreeNode(int val) {
        this.val = val;

    }

}
*/
public class Solution {
    ArrayList<ArrayList<Integer>> result = new ArrayList<>();
    ArrayList<Integer> array = new ArrayList<>();
    public ArrayList<ArrayList<Integer>> FindPath(TreeNode root,int target) {
         if(null == root){
           return result;
        }
        array.add(root.val);
        target-=root.val;
        if(0 == target && null == root.left && null == root.right){
            result.add(new ArrayList<>(array)); //直接赋值是引用赋值,指向相同对象。array最终回到顶层就只有一个10。
        }
        FindPath(root.left,target);
        FindPath(root.right,target);
        array.remove(array.size() -1);
        return result;
    }
}

非递归法:注意牛客的编译器C++越界不会报错。但是java肯定会抛异常的。一定小心数组越界。

import java.util.ArrayList;
import java.util.Stack;
/**
public class TreeNode {
    int val = 0;
    TreeNode left = null;
    TreeNode right = null;

    public TreeNode(int val) {
        this.val = val;

    }

}
*/
public class Solution {
   
    
    public ArrayList<ArrayList<Integer>> FindPath(TreeNode root,int target) {
         ArrayList<ArrayList<Integer>> result = new ArrayList<>();
         if(null == root){
           return result;
        }
        ArrayList<Integer> array = new ArrayList<>();
        Stack<TreeNode> stack = new Stack<>();
        stack.push(root);
        TreeNode cur = root;
        TreeNode last = null;
        int sum=0;
        while(!stack.isEmpty()){
            if(null != cur){
                stack.push(cur);
                array.add(cur.val);
                sum += cur.val;
                if(target == sum && null == cur.left && null == cur.right){
                    result.add(new ArrayList<Integer>(array));
                }
                cur = cur.left;
            }else{
                TreeNode temp = stack.peek();
                if(null != temp.right && temp.right != last){
                    cur = temp.right;
                }else{
                    last = temp;
                    stack.pop();
                    if(array.size() >0){
                        array.remove(array.size() - 1);
                    }
                    sum -= temp.val;
                }
            }
            
        }
        return result;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值