标签:public null iii tree node highlight int div code sum
样例:root = [10,5,-3,3,2,null,11,3,-2,null,1], sum = 7,一开始以为10 5 -3也算一种情况,但实际上是不算的。
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
int ans = 0;
int pos;
int solve(TreeNode root, int sum){
if(root == null) return 0;
return (root.val == sum?1:0)+solve(root.left, sum-root.val)
+solve(root.right, sum-root.val);
}
public int pathSum(TreeNode root, int sum) {
if(root == null) return 0;
return solve(root,sum)+pathSum(root.left,sum)+pathSum(root.right, sum);
}
}
标签:public null iii tree node highlight int div code sum
原文地址:https://www.cnblogs.com/ctqchina/p/10111422.html