113. 路径总和 II + 递归 + dfs

113. 路径总和 II

LeetCode_113

题目描述

113. 路径总和 II + 递归 + dfs

解法一:低效率的递归回溯

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    List<List<Integer>> result = new ArrayList<>();
    int target;
    public List<List<Integer>> pathSum(TreeNode root, int targetSum) {
        if(root == null)
            return result;
        this.target = targetSum;
        LinkedList<Integer> list = new LinkedList<>();
        list.push(root.val);
        dfs(root, root.val, list);
        return result;
    }
    public void dfs(TreeNode root, int current, LinkedList<Integer> list){
        //if(current > target)
          //  return;
        if(root.left == null && root.right == null){
            if(current == target){
                LinkedList<Integer> nowlist = new LinkedList<>(list);
                Collections.reverse(nowlist);
                result.add(nowlist);
            }
            return;
        }
        if(root.left != null){
            list.push(root.left.val);
            dfs(root.left, current + root.left.val, list);
            list.pop();
        }
        if(root.right != null){
            list.push(root.right.val);
            dfs(root.right, current + root.right.val, list);
            list.pop();
        }
    }
}

解法二:更加高效的回溯

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    List<List<Integer>> result = new ArrayList<>();
    int target;
    public List<List<Integer>> pathSum(TreeNode root, int targetSum) {
        if(root == null)
            return result;
        this.target = targetSum;
        LinkedList<Integer> list = new LinkedList<>();
        dfs(root, 0, list);
        return result;
    }
    public void dfs(TreeNode root, int current, LinkedList<Integer> list){
        if(root == null)
            return;
        list.push(root.val);

        current += root.val;
        if(root.left == null && root.right == null){//是叶子结点
            if(current == target){
                LinkedList<Integer> nowlist = new LinkedList<>(list);
                Collections.reverse(nowlist);
                result.add(nowlist);
            }
            list.pop();
            return;
        }
        dfs(root.left, current, list);
        dfs(root.right, current, list);
        
        list.pop();
    }
}
上一篇:海信欧洲区营业收入激增113%,成功巩固在该地区的竞争优势和盈利能力


下一篇:MATLAB从入门到实战【Matlab 113期】