力扣 二叉树中的最大路径和

题目

47 二叉树中的最大路径和

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

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

问题描述 :

给定一个非空二叉树,返回其最大路径和。

本题中,路径被定义为一条从树中任意节点出发,达到任意节点的序列。该路径至少包含一个节点,且不一定经过根节点。

示例 1:

输入: [1,2,3]

   1

  / \

 2   3

输出: 6

示例 2:

输入: [-10,9,20,null,null,15,7]

-10

/ \

9 20

 /  \

15 7

输出: 42

可使用以下main函数:

#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 res=Solution().maxPathSum(root);

cout<<res<<endl;

}

输入说明 :

首先输入结点的数目n(注意,这里的结点包括题中的null空结点)

然后输入n个结点的数据,需要填充为空的结点,输入null。

输出说明 :

输出一个整数,表示结果。

输入输出

7
-10 9 20 null null 15 7

42

Code


```cpp
#include <iostream>

#include <queue>

#include <cstdlib>

#include <cstring>

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{
private:
    int maxSum=0;
public:
    int maxPathSum(TreeNode*root){
        maxGain(root );
        return  maxSum;
    }
    int maxGain(TreeNode*root ){
         if(root== nullptr)
             return 0;
        //计算左右节点的最大贡献值,只有最大贡献值>0才要
        int leftGain=max(maxGain(root->left ),0);
        int rightGain=max(maxGain(root->right),0);
        if(maxSum< leftGain+rightGain+root->val){
            maxSum=leftGain+rightGain+root->val;
        }
        //返回节点的最大贡献值
        return max(leftGain,rightGain)+root->val;


    }


//    int maxGain(TreeNode*root){
//        if(root= nullptr){
//            return 0;
//        }
//        return root->val+  maxGain(root->left), maxGain(root->right ;
//    }

};


int main()

{

    TreeNode* root;

    root=inputTree();

    int res=Solution().maxPathSum(root);

    cout<<res<<endl;

}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值