leetcode 之Path Sum II

来源:互联网 发布:网络安全教育新闻稿 编辑:程序博客网 时间:2024/05/22 09:41

Given a binary tree and a sum, find all root-to-leaf paths where each path’s sum equals the given sum.

For example:
Given the below binary tree and sum = 22,
5
/ \
4 8
/ / \
11 13 4
/ \ / \
7 2 5 1
return
[
[5,4,11,2],
[5,8,4,5]
]

树结构入门题,一直没怎么做树结构的。今天特地找来做做看

有些代码早已忘记,但是我现在还记得~–思路~

    public static void pre(TreeNode root){        System.out.print(root.val);        if (root.left!=null) {            pre(root.left);        }        if (root.right!=null) {            pre(root.right);        }    }    public static void mid(TreeNode root){        if (root.left!=null) {            mid(root.left);        }        System.out.print(root.val+"\n");        if (root.right!=null) {            mid(root.right);        }    }    public static void last(TreeNode root){        if (root.left!=null) {            last(root.left);        }        if (root.right!=null) {            last(root.right);        }        System.out.print(root.val+"\n");    }

然后看题意,结果里的数组是从根开始的,所以我们理所当然的选择用先根遍历。

然后先想办法输出所有的路径

public static void pre(TreeNode root, List<Integer> path) {        path.add(root.val);        if (root.left != null) {            if (root.right != null) {                pre(root.left, new ArrayList<Integer>(path));            }            else{                pre(root.left,path);            }        } else {            pathsum.add(path);        }        if (root.right != null) {            pre(root.right, new ArrayList<Integer>(path));        } else {            pathsum.add(path);        }    }

reSult:

[[5, 4, 11, 7], [5, 4, 11, 7], [5, 4, 11, 2], [5, 4, 11, 2], [5, 4, 11], [5, 8, 13], [5, 8, 13], [5, 8, 4, 5], [5, 8, 4, 5], [5, 8, 4, 1], [5, 8, 4, 1]]

结果集没有问题,问题是每个路径都入了两次。
所以。删一个add就可以了。因为证明两个add加的list是一样的
删除哪个都行。
第二次代码。

public static void pre(TreeNode root, List<Integer> path) {        path.add(root.val);        if (root.left != null) {            if (root.right != null) {                pre(root.left, new ArrayList<Integer>(path));            }            else{                pre(root.left,path);            }        }        if (root.right != null) {            pre(root.right, new ArrayList<Integer>(path));        } else {            pathsum.add(path);        }    }

根据题意我们要算路径和。当然不能最后再遍历那个list求和了。空间换时间思路。

public static void pre(TreeNode root, List<Integer> path,int sum) {        path.add(root.val);        sum+=root.val;        if (root.left != null) {            if (root.right != null) {                pre(root.left, new ArrayList<Integer>(path),sum);            }            else{                pre(root.left,path,sum);            }        }        if (root.right != null) {            pre(root.right, new ArrayList<Integer>(path),sum);        } else {            if (sum==22) {                pathsum.add(path);            }        }    }

二次修改 ,结果
[[5, 4, 11, 2], [5, 8, 4, 5]]

为了一次通过,肯定要有特殊情况的排查。所以要加上
if (root==null) {
return ;
}

在提交的时候发现,本算法并不适应 只一个根和一个左结点的数。也就是不适应二层及以下的树。

问题在于有一行代码仍然传的是实参。也就是指针(引用)。

提交之后很困惑。因为它告诉我
Input: {1,2}, 1
Output: [[1]]
Expected: []

这不应该是1么。为什么答案应该是空?
想了一下应该是根到叶。也就是说path.size应该大于1?

然后。。
Input: {1}, 1
Output: []。
Expected: [[1]]

一个根也可以啊!!

那就是说有叶的时候要一直到叶。。
那就加上root.right==root.left==null
最终代码。通过了的

/** * Definition for binary tree * public class TreeNode { *     int val; *     TreeNode left; *     TreeNode right; *     TreeNode(int x) { val = x; } * } */import java.util.ArrayList;import java.util.List;public class Solution {    public List<List<Integer>> pathSum(TreeNode root, int sum) {        this.target=sum;        pre(root, new ArrayList<Integer>(),0);        return pathsum;    }    List<List<Integer>> pathsum = new ArrayList<List<Integer>>();    int target;    public  void pre(TreeNode root, List<Integer> path,int sum) {        if (root==null) {            return ;        }        path.add(root.val);        sum+=root.val;        if (sum==target&&root.left==null&&root.right==null) {                pathsum.add(path);        }        if (root.left != null) {                pre(root.left, new ArrayList<Integer>(path), sum);        }        if (root.right != null) {            pre(root.right, new ArrayList<Integer>(path),sum);        }    }}
0 0