[LeetCode]*124.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);
}


【温故】

/*---------------------------------------
*   日期:2015-04-30
*   作者:SJF0115
*   题目: 124.Binary Tree Maximum Path Sum
*   网址:https://leetcode.com/problems/binary-tree-maximum-path-sum/
*   结果:AC
*   来源:LeetCode
*   博客:
-----------------------------------------*/
#include <iostream>
#include <vector>
using namespace std;

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

class Solution {
public:
    int maxPathSum(TreeNode *root) {
        if(root == NULL){
            return 0;
        }//if
        int maxSum = INT_MIN;
        maxPath(root,maxSum);
        return maxSum;
    }
private:
    int maxPath(TreeNode *root,int &maxSum){
        if(root == NULL){
            return 0;
        }//if
        // 后序遍历
        // root左子树最大路径值
        int leftMax = maxPath(root->left,maxSum);
        // root右子树最大路径值
        int rightMax = maxPath(root->right,maxSum);
        // 双侧最大路径值
        int curMax = root->val;
        if(leftMax > 0){
            curMax += leftMax;
        }//if
        if(rightMax > 0){
            curMax += rightMax;
        }//if
        maxSum = max(maxSum,curMax);
        // 如果是某节点的子树时只能返回单侧最大路径值
        int oneSideMax = max(leftMax,rightMax);
        if(oneSideMax > 0){
            return root->val + oneSideMax;
        }//if
        else{
            return root->val;
        }
    }
};

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

int main() {
    Solution solution;
    TreeNode* root(0);
    CreateBTree(root);
    cout<<solution.maxPathSum(root);
}


目录
相关文章
|
5月前
Leetcode Minimum Depth of Binary Tree (面试题推荐)
计算树的最小深度 很简单的一道题,只需要遍历一次树,到叶子节点的时候计算一下深度和当前最小深度比较,保存最小值就行。 我在这用了一个全局变量 mindepth。总感觉我这代码写的不够简练,求更精简的方法。
24 0
|
5月前
Leetcode 236. Lowest Common Ancestor of a Binary Tree
根据LCA的定义,二叉树中最小公共祖先就是两个节点p和q最近的共同祖先节点,LCA的定义没什么好解释的,主要是这道题的解法。
16 0
LeetCode contest 190 5418. 二叉树中的伪回文路径 Pseudo-Palindromic Paths in a Binary Tree
LeetCode contest 190 5418. 二叉树中的伪回文路径 Pseudo-Palindromic Paths in a Binary Tree
|
算法 Python
LeetCode 108. 将有序数组转换为二叉搜索树 Convert Sorted Array to Binary Search Tree
LeetCode 108. 将有序数组转换为二叉搜索树 Convert Sorted Array to Binary Search Tree
LeetCode 102. 二叉树的层序遍历 Binary Tree Level Order Traversal
LeetCode 102. 二叉树的层序遍历 Binary Tree Level Order Traversal
LeetCode 104. 二叉树的最大深度 Maximum Depth of Binary Tree
LeetCode 104. 二叉树的最大深度 Maximum Depth of Binary Tree
LeetCode Contest 178-1367. 二叉树中的列表 Linked List in Binary Tree
LeetCode Contest 178-1367. 二叉树中的列表 Linked List in Binary Tree
|
30天前
|
机器学习/深度学习 算法
力扣刷题日常(一)
力扣刷题日常(一)
20 2
|
1月前
|
存储 索引
《LeetCode》—— LeetCode刷题日记
《LeetCode》—— LeetCode刷题日记
|
1月前
|
搜索推荐
《LeetCode》——LeetCode刷题日记3
《LeetCode》——LeetCode刷题日记3