leetcode 437. Path Sum III
发布时间:2020-12-14 04:27:52 所属栏目:大数据 来源:网络整理
导读:样例:root = [10,5,-3,3,2,null,11,-2,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
样例:root = [10,5,-3,3,2,null,11,-2,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); } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |