题目描述(简单难度)
给定一个sum,判断是否有一条从根节点到叶子节点的路径,该路径上所有数字的和等于sum。
解法一 递归
这道题其实和 111 题 是一样的,大家可以先看 111 题 的分析,这道题无非是把 111 题 递归传递的depth改为了sum的传递。
如果不仔细分析题目,代码可能会写成下边的样子。
public boolean hasPathSum(TreeNode root, int sum) {
if (root == null) {
return false;
}
return hasPathSumHelper(root, sum);
}
private boolean hasPathSumHelper(TreeNode root, int sum) {
if (root == null) {
return sum == 0;
}
return hasPathSumHelper(root.left, sum - root.val) || hasPathSumHelper(root.right, sum - root.val);
}
看起来没什么问题,并且对于题目给的样例也是没问题的。但是对于下边的样例:
3
/ \
9 20
/ / \
8 15 7
sum = 12
当某个子树只有一个孩子的时候,就会出问题了,可以看 111 题 的分析。
所以代码需要写成下边的样子。
class TreeNode{
int val;
TreeNode left;
TreeNode right;
TreeNode(int x){val=x;}
}
public class Path_Sum {
public static boolean hasPathSum(TreeNode root,int sum) {
if(root==null) return false;
return hasPathSumHelper(root,sum);
}
private static boolean hasPathSumHelper(TreeNode root,int sum) {
if(root.left==null && root.right==null) {
return root.val==sum;
}
if(root.left==null) {
return hasPathSumHelper(root.right,sum-root.val);
}
if(root.right==null) {
return hasPathSumHelper(root.left,sum-root.val);
}
return hasPathSumHelper(root.left,sum-root.val)||hasPathSumHelper(root.right,sum-root.val);
}
public static void main(String args[]) {
TreeNode[] node=new TreeNode[9];
node[0]=new TreeNode(5);
node[1]=new TreeNode(4);
node[2]=new TreeNode(8);
node[3]=new TreeNode(11);
node[4]=new TreeNode(13);
node[5]=new TreeNode(4);
node[6]=new TreeNode(7);
node[7]=new TreeNode(2);
node[0].left=node[1];
node[0].right=node[2];
node[1].left=node[3];
node[2].left=node[4];
node[2].right=node[5];
node[3].left=node[6];
node[3].right=node[7];
node[5].right=node[8];
int sum=26;
boolean ans=hasPathSum(node[0],sum);
System.out.println(ans);
}
}
解法二 BFS
同样的,我们可以利用一个队列对二叉树进行层次遍历。同时还需要一个队列,保存当前从根节点到当前节点已经累加的和。BFS的基本框架不用改变,参考 102 题。只需要多一个队列,进行细微的改变即可。
public boolean hasPathSum(TreeNode root, int sum) {
Queue<TreeNode> queue = new LinkedList<TreeNode>();
Queue<Integer> queueSum = new LinkedList<Integer>();
if (root == null)
return false;
queue.offer(root);
queueSum.offer(root.val);
while (!queue.isEmpty()) {
int levelNum = queue.size(); // 当前层元素的个数
for (int i = 0; i < levelNum; i++) {
TreeNode curNode = queue.poll();
int curSum = queueSum.poll();
if (curNode != null) {
//判断叶子节点是否满足了条件
if (curNode.left == null && curNode.right == null && curSum == sum) {
return true;
}
//当前节点和累计的和加入队列
if (curNode.left != null) {
queue.offer(curNode.left);
queueSum.offer(curSum + curNode.left.val);
}
if (curNode.right != null) {
queue.offer(curNode.right);
queueSum.offer(curSum + curNode.right.val);
}
}
}
}
return false;
}
解法三 DFS
解法一其实本质上就是做了DFS,我们知道DFS可以用栈去模拟。对于这道题,我们可以像解法二的BFS一样,再增加一个栈,去保存从根节点到当前节点累计的和就可以了。
这里的话,用DFS里的中序遍历,参考 94 题。
public boolean hasPathSum(TreeNode root, int sum) {
Stack<TreeNode> stack = new Stack<>();
Stack<Integer> stackSum = new Stack<>();
TreeNode cur = root;
int curSum = 0;
while (cur != null || !stack.isEmpty()) {
// 节点不为空一直压栈
while (cur != null) {
stack.push(cur);
curSum += cur.val;
stackSum.push(curSum);
cur = cur.left; // 考虑左子树
}
// 节点为空,就出栈
cur = stack.pop();
curSum = stackSum.pop();
//判断是否满足条件
if (curSum == sum && cur.left == null && cur.right == null) {
return true;
}
// 考虑右子树
cur = cur.right;
}
return false;
}
参考文献
1.https://zhuanlan.zhihu.com/p/75124813