思路:
- 每次访问子树的时候,都要用sum减去当前节点的val值。
- 叶子节点是指左右子树都为null,即为我们的base case,判断当目前为止sum是否为0。
- 如果左子树已经找到了sum为0的,或者右子树已经找到了sum为0的,则返回true。
package vip.wulang.leetcode.problem112;
import vip.wulang.leetcode.structrue.TreeNode;
public class Solution {
public boolean hasPathSum(TreeNode root, int sum) {
if (root == null) {
return false;
}
return f(root, sum).find;
}
private static class Info {
public boolean find;
public Info(boolean find) {
this.find = find;
}
}
private Info f(TreeNode node, int sum) {
if (node.left == null && node.right == null) {
sum -= node.val;
return new Info(sum == 0);
}
Info left = null;
if (node.left != null) {
left = f(node.left, sum - node.val);
}
Info right = null;
if (node.right != null) {
right = f(node.right, sum - node.val);
}
if ((left != null && left.find) || (right != null && right.find)) {
return new Info(true);
} else {
return new Info(false);
}
}
}