LeetCode 路径总合III

LeetCode 路径总合III

题目

44 路径总和 III

作者: Turbo时间限制: 1S章节: DS:树

晚于: 2022-08-04 23:55:00后提交分数乘系数50%

问题描述 :

给定一个二叉树,它的每个结点都存放着一个整数值。

找出路径和等于给定数值的路径总数。

路径不需要从根节点开始,也不需要在叶子节点结束,但是路径方向必须是向下的(只能从父节点到子节点)。

二叉树不超过1000个节点,且节点数值范围是 [-1000000,1000000] 的整数。

示例:

root = [10,5,-3,3,2,null,11,3,-2,null,1], sum = 8

  10

 /  \

5   -3

/ \ \

3 2 11

/ \ \

3 -2 1

返回 3。和等于 8 的路径有:

  1. 5 -> 3

  2. 5 -> 2 -> 1

  3. -3 -> 11

可使用以下main函数:

#include

#include

#include

#include

#include

using namespace std;

struct TreeNode

{

int val;

TreeNode *left;

TreeNode *right;

TreeNode() : val(0), left(NULL), right(NULL) {}

TreeNode(int x) : val(x), left(NULL), right(NULL) {}

TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}

};

TreeNode* inputTree()

{

int n,count=0;

char item[100];

cin>>n;

if (n==0)

    return NULL;

cin>>item;

TreeNode* root = new TreeNode(atoi(item));

count++;

queue<TreeNode*> nodeQueue;

nodeQueue.push(root);
while (count<n)
{

    TreeNode* node = nodeQueue.front();

    nodeQueue.pop();

    cin>>item;

    count++;

    if (strcmp(item,"null")!=0)

    {

        int leftNumber = atoi(item);

        node->left = new TreeNode(leftNumber);

        nodeQueue.push(node->left);

    }

    if (count==n)

        break;

    cin>>item;

    count++;

    if (strcmp(item,"null")!=0)

    {

        int rightNumber = atoi(item);

        node->right = new TreeNode(rightNumber);

        nodeQueue.push(node->right);

    }

}

return root;

}

int main()

{

TreeNode* root;

root=inputTree();

int sum;

cin>>sum;

int res=Solution().pathSum(root,sum);

cout<<res<<endl;

}

代码实现


```cpp

```cpp

```cpp
#include <iostream>

#include <queue>

#include <cstdlib>

#include <cstring>

#include<map>

using namespace std;

struct TreeNode

{

    int val;

    TreeNode *left;

    TreeNode *right;

    TreeNode() : val(0), left(NULL), right(NULL) {}

    TreeNode(int x) : val(x), left(NULL), right(NULL) {}

    TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}

};

TreeNode* inputTree()

{

    int n,count=0;

    char item[100];

    cin>>n;

    if (n==0)

        return NULL;

    cin>>item;

    TreeNode* root = new TreeNode(atoi(item));

    count++;

    queue<TreeNode*> nodeQueue;

    nodeQueue.push(root);

    while (count<n)

    {

        TreeNode* node = nodeQueue.front();

        nodeQueue.pop();

        cin>>item;

        count++;

        if (strcmp(item,"null")!=0)

        {

            int leftNumber = atoi(item);

            node->left = new TreeNode(leftNumber);

            nodeQueue.push(node->left);

        }

        if (count==n)

            break;

        cin>>item;

        count++;

        if (strcmp(item,"null")!=0)

        {

            int rightNumber = atoi(item);

            node->right = new TreeNode(rightNumber);

            nodeQueue.push(node->right);

        }

    }

    return root;

}


class Solution {
public:
    int pathSum(TreeNode* root, int targetSum) {
          if(root== nullptr)
              return 0;
        return pathSum(root->left,targetSum)+ pathSum(root->right,targetSum)+  pathSumStartWithRoot(root,targetSum);
        //纯左+纯右+带根节点
    }
    int pathSumStartWithRoot(TreeNode*root,int targetSum){
        if(!root){
            return 0;
        }
        int count =0 ;
        if(root->val==targetSum)
            count++;
        count+= pathSumStartWithRoot(root->left,targetSum-root->val);
        count+= pathSumStartWithRoot(root->right,targetSum-root->val);
        return count;
    }
};
int main()

{

    TreeNode* root;

    root=inputTree();

    int sum;

    cin>>sum;

    int res=Solution().pathSum(root,sum);

    cout<<res<<endl;

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值