跳至主要內容

113_路径总和II

T4mako算法深度优先二叉树回溯小于 1 分钟

113_路径总和II

中等
class Solution {
    public List<List<Integer>> pathSum(TreeNode root, int targetSum) {
       List<List<Integer>> res = new ArrayList<>();
        if(root == null){
            return res;
        }
        List list = new ArrayList();
        list.add(root.val);
        path(root,root.val,targetSum,res,list);
        return res;
    }
    public void path(TreeNode root, int sum,int target, List<List<Integer>> res,List<Integer> list){
        if(root.left != null){
            list.add(root.left.val);
            sum += root.left.val;
            TreeNode temp = root.left;
            path(temp,sum,target,res,list);
            sum -= list.get(list.size() - 1);
            list.remove(list.size() - 1);
        }
        if(root.right != null){
            list.add(root.right.val);
            sum += root.right.val;
            TreeNode temp = root.right;
            path(temp,sum,target,res,list);
            sum -= list.get(list.size() - 1);
            list.remove(list.size() - 1);
        }
        if(root.left == null && root.right == null && sum == target){
            res.add(new ArrayList<>(list));
        }
    }
}

深度优先遍历每个节点,计算它们的求和值是否与目标一致,一致就将数组加入到res中

评论
  • 按正序
  • 按倒序
  • 按热度
Powered by Waline v2.15.5