112路径总和 一道不错的递归题
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| class Solution {
public boolean digui(TreeNode node,int cumulativeDistance,int sum) {
if(node==null) return false; if(node.left==null&&node.right==null) { if(cumulativeDistance+node.val==sum) return true; else return false; }
return digui(node.left,cumulativeDistance+node.val,sum)||digui(node.right,cumulativeDistance+node.val,sum);
} public boolean hasPathSum(TreeNode root, int sum) {
return digui(root,0,sum);
} }
|
最后更新时间: