113. Path Sum II

找从头到尾SUM=TARGET的路径

就RECURSION就行

注意得SUM==TARGET 还得正好是ROOT-LEAF
然后没啥难的了 好像可以用数据结构做 比如LINKEDLIST不过貌似还是得迭代。。

public List<List<Integer>> pathSum(TreeNode root, int sum) {
        List<List<Integer>> res = new ArrayList<List<Integer>> ();

        if(root == null) return res;
        

        helper(root,sum,res, new ArrayList<Integer>());

        
      	return res;

    }

    public void helper(TreeNode root, int sum, List<List<Integer>> res, List<Integer> tempList)
    {
        //System.out.println(root.val + " " + sum);
    	if(root.left == null && root.right == null && root.val == sum)
    	{
    		tempList.add(root.val);
    		res.add(new ArrayList<Integer>(tempList));
    		return;
    	}
    	else
    	{
    		tempList.add(root.val);

    		if(root.left != null) helper(root.left,sum-root.val,res,new ArrayList<Integer>(tempList));
    		if(root.right != null) helper(root.right,sum-root.val,res, new ArrayList<Integer>(tempList));
    		return;
    	}
 
    }




二刷。

先Recursion做了一下。

public class Solution 
{

    public List<List<Integer>> pathSum(TreeNode root, int sum) 
    {
        List<List<Integer>> res = new ArrayList<List<Integer>>();
        if(root == null) return res;
        List<Integer> tempList = new ArrayList<>();
        tempList.add(root.val);
        
        helper(res,root,sum-root.val,tempList);
        
        return res;
    }
    
    public void helper(List<List<Integer>> res, TreeNode root, int sum, List<Integer> tempList)
    {
        if(sum == 0 && root.left == null && root.right == null) res.add(new ArrayList<>(tempList));
        else
        {
            if(root.left != null)
            {
                tempList.add(root.left.val);
                helper(res,root.left,sum-root.left.val,new ArrayList<>(tempList));
                tempList.remove(tempList.size()-1);
            }
            if(root.right != null)
            {
                tempList.add(root.right.val);
                helper(res,root.right,sum-root.right.val,new ArrayList<>(tempList));
            }
        }
        
        
    }
}

再试试Iterative.

失败。。

Discussion里大同小异。都是recursiion.



三刷。

DFS就行了,白送的题。

Recursion:

public class Solution {
    public List<List<Integer>> pathSum(TreeNode root, int sum) {
        List<List<Integer>> res = new ArrayList<>();
        if (root == null) return res;
        dfs(res, root, sum, new ArrayList<>());
        return res;
    }
    
    public void dfs(List<List<Integer>> res, TreeNode root, int sum, List<Integer> tempList) {
        if (root == null) return;
        tempList.add(root.val);
        if (root.left == null && root.right == null) {
            if (sum - root.val == 0) {
                res.add(new ArrayList<>(tempList));
            }
        } else {
            dfs(res, root.left, sum - root.val, tempList);
            dfs(res, root.right, sum - root.val, tempList);
        }
        tempList.remove(tempList.size() - 1);
    }
}

Iteration:

尝试了一下不好做,tempList里的元素添加顺序要求是preOrder的,但是遍历顺序只能满足一跳路径是如此。

只能找到最左那条路,如果用iteration.

原文地址:https://www.cnblogs.com/reboot329/p/6116768.html