题目描述
You are given a binary tree in which each node contains an integer value.
Find the number of paths that sum to a given value.
The path does not need to start or end at the root or a leaf, but it must go downwards (traveling only from parent nodes to child nodes).
The tree has no more than 1,000 nodes and the values are in the range -1,000,000 to 1,000,000.
Example:
root = [10,5,-3,3,2,null,11,3,-2,null,1], sum = 8
10
/ \
5 -3
/ \
3 2 11
/ \
3 -2 1
Return 3. The paths that sum to 8 are:
- 5 -> 3
- 5 -> 2 -> 1
- -3 -> 11
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/path-sum-iii
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
代码
一个递归算法中嵌套另一个递归算法
#include <iostream>
using namespace std;
/// Definition for a binary tree node.
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
/// Recursive
/// Time Complexity: O(n), where n is the node's number of the tree
/// Space Complexity: O(h), where h is the height of the tree
class Solution {
public:
// 以root为根节点的二叉树中,寻找和为sum的路径,返回路径个数
int pathSum(TreeNode* root, int sum) {
if(root == NULL)
return 0;
return findPath(root, sum) //包含当前root结点,其和为sum的路径个数
+ pathSum(root->left , sum) // 不包含当前root结点,且其和为sum的路径个数
+ pathSum(root->right , sum);
}
private:
//以node为根节点的路径中,寻找包含node且和为num的路径
//返回路径个数
int findPath(TreeNode* node, int num){
if(node == NULL)
return 0;
int res = 0;
if(node->val == num)
res += 1; //不直接返回1是因为节点数值存在负数,接下来可能还存在和为num的路径
res += findPath(node->left , num - node->val);
res += findPath(node->right , num - node->val);
return res;
}
};
int main() {
/*****************
* Test case:
*
* 10
* / \
* 5 -3
* / \ \
* 3 2 11
* / \ \
* 3 -2 1
*****************/
TreeNode* node1 = new TreeNode(3);
TreeNode* node2 = new TreeNode(-2);
TreeNode* node3 = new TreeNode(3);
node3->left = node1;
node3->right = node2;
TreeNode* node4 = new TreeNode(1);
TreeNode* node5 = new TreeNode(2);
node5->right = node4;
TreeNode* node6 = new TreeNode(5);
node6->left = node3;
node6->right = node5;
TreeNode* node7 = new TreeNode(11);
TreeNode* node8 = new TreeNode(-3);
node8->right = node7;
TreeNode* node9 = new TreeNode(10);
node9->left = node6;
node9->right = node8;
cout << Solution().pathSum(node9, 8) << endl;
return 0;
}