[LeetCode]Binary Tree Maximum Path Sum

【题目】

Given a binary tree, find the maximum path sum.

The path may start and end at any node in the tree.

For example:

Given the below binary tree,

       1
      /      2   3

Return 6.

【分析】

  

需要考虑以上两种情况:

1 左子树或者右子树中存有最大路径和 不能和根节点形成一个路径

2 左子树 右子树 和根节点形成最大路径

【代码】

/*********************************
*   日期:2014-12-23
*   作者:SJF0115
*   题号: Binary Tree Maximum Path Sum
*   来源:https://oj.leetcode.com/problems/binary-tree-maximum-path-sum/
*   结果:AC
*   来源:LeetCode
*   总结:
**********************************/
#include <iostream>
#include <climits>
#include <algorithm>
using namespace std;

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

class Solution {
public:
    int maxPathSum(TreeNode *root) {
        if(root == NULL){
            return 0;
        }//if
        maxSum = INT_MIN;
        maxPath(root);
        return maxSum;
    }
private:
    int maxSum;
    int maxPath(TreeNode *node){
        if(node == NULL){
            return 0;
        }//if
        // 左子树最大路径值(路径特点:左右节点只能选一个)
        int leftMax = maxPath(node->left);
        // 右子树最大路径值(路径特点:左右节点只能选一个)
        int rightMax = maxPath(node->right);

        // 以node节点的双侧路径((node节点以及左右子树))
        int curMax = node->val;
        if(leftMax > 0){
            curMax += leftMax;
        }//if
        if(rightMax > 0){
            curMax += rightMax;
        }//if
        maxSum = max(curMax,maxSum);
        // 以node节点的单侧路径(node节点以及左右子树的一个)
        if(max(leftMax,rightMax) > 0){
            return max(leftMax,rightMax) + node->val;
        }
        else{
            return node->val;
        }
    }
};

//按先序序列创建二叉树
int CreateBTree(TreeNode*& T){
    int data;
    //按先序次序输入二叉树中结点的值,-1表示空树
    cin>>data;
    if(data == -1){
        T = NULL;
    }
    else{
        T = (TreeNode*)malloc(sizeof(TreeNode));
        //生成根结点
        T->val = data;
        //构造左子树
        CreateBTree(T->left);
        //构造右子树
        CreateBTree(T->right);
    }
    return 0;
}

int main() {
    Solution solution;
    TreeNode* root(0);
    CreateBTree(root);
    cout<<solution.maxPathSum(root);
}
时间: 2024-10-22 18:00:55

[LeetCode]Binary Tree Maximum Path Sum的相关文章

LeetCode: Binary Tree Maximum Path Sum [124]

[题目] Given a binary tree, find the maximum path sum. The path may start and end at any node in the tree. For example: Given the below binary tree, 1 / 2 3 Return 6. [题意] 给定一棵二叉树,找出其中路径和最大的路径,然会返回最大路径和. 本题中的路径不是从根节点到叶子节点这样的传统的路径,而是指的二叉树中任意两个节点之间的联通路径.

[LeetCode] Binary Tree Maximum Path Sum(递归)

Given a binary tree, find the maximum path sum. The path may start and end at any node in the tree. For example: Given the below binary tree, 1 / 2 3 Return 6. /** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNo

LeetCode: Binary Tree Maximum Path Sum 解题报告

Binary Tree Maximum Path SumGiven a binary tree, find the maximum path sum. The path may start and end at any node in the tree. For example:Given the below binary tree, 1      / \     2   3 SOLUTION 1: 计算树的最长path有2种情况: 1. 通过根的path. (1)如果左子树从左树根到任何一个N

LeetCode——Binary Tree Maximum Path Sum

Given a binary tree, find the maximum path sum. The path may start and end at any node in the tree. For example: Given the below binary tree, 1 / 2 3 Return 6. 原题链接:https://oj.leetcode.com/problems/binary-tree-maximum-path-sum/ 题目:给定一二叉树,求出最大路径和. 分析:

[LeetCode] Binary Tree Maximum Path Sum(最大路径和)

Given a binary tree, find the maximum path sum. The path may start and end at any node in the tree. For example:Given the below binary tree, 1 / 2 3 Return 6. 题目意思很简单,就是给定一棵二叉树,求最大路径和.path 可以从任意 node 开始,到任意 node 结束. 这道题在 LeetCode 上的通过率只有 20% 多一点,并被标记

[Leetcode] Binary tree maximum path sum求二叉树最大路径和

Given a binary tree, find the maximum path sum. The path may start and end at any node in the tree. For example:Given the below binary tree, 1 / 2 3 Return6. 思路:题目中说明起始节点可以是任意节点,所以,最大的路径和不一样要经过root,可以是左子树中某一条,或者是右子树中某一条,当然也可能是经过树的根节点root的.递归式是应该是这三者中

[LeetCode] Binary Tree Maximum Path Sum 求二叉树的最大路径和

Given a binary tree, find the maximum path sum. The path may start and end at any node in the tree. For example:Given the below binary tree, 1 / 2 3 Return 6. 这道求二叉树的最大路径和是一道蛮有难度的题,难就难在起始位置和结束位置可以为任意位置,我当然是又不会了,于是上网看看大神们的解法,看了很多人的都没太看明白,最后发现了网友Yu's C

leetcode&mdash;&mdash;Binary Tree Maximum Path Sum

    这题很难,主要是我没理解题目的意思,后来看着给的测试参考,和网上找的答案,终于理解了. 例如: 给的 正确的路线应该是   也就是说,路径是一条的,不是有分叉的,之所以算出55是因为把>0的数都加上去了,这样路径就分叉了,就不是一条路径了,所以用dfs做时,返回值应该是左子树或者右子树中>0且比较大的那个子树的路径的值加上当前根节点的值. 我的是参考网上答案的代码: // 用dfs,因为路径是任意的,所以根节点也是任意的,如果左子树的值>0,即对sum有贡献,则sum加其值,右子

leetcode -day9 Candy &amp; Gas Station &amp; Binary Tree Maximum Path Sum

1.  Candy There are N children standing in a line. Each child is assigned a rating value. You are giving candies to these children subjected to the following requirements: Each child must have at least one candy. Children with a higher rating get m