[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);
}

时间: 2024-12-22 18:02:58

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

[LeetCode] Print Binary Tree 打印二叉树

Print a binary tree in an m*n 2D string array following these rules: The row number m should be equal to the height of the given binary tree. The column number n should always be an odd number. The root node's value (in string format) should be put i

[LeetCode]94.Binary Tree Inorder Traversal

[题目] Given a binary tree, return the inorder traversal of its nodes' values. For example: Given binary tree {1,#,2,3}, 1 \ 2 / 3 return [1,3,2]. Note: Recursive solution is trivial, could you do it iteratively? confused what "{1,#,2,3}" means? &

[LeetCode]144.Binary Tree Preorder Traversal

[题目] Given a binary tree, return the preorder traversal of its nodes' values. For example: Given binary tree {1,#,2,3}, 1 \ 2 / 3 return [1,2,3]. Note: Recursive solution is trivial, could you do it iteratively? [代码一] /*******************************

[LeetCode]199.Binary Tree Right Side View

题目 Given a binary tree, imagine yourself standing on the right side of it, return the values of the nodes you can see ordered from top to bottom. For example: Given the following binary tree, 1 <- / \ 2 3 <- \ \ 5 4 <- You should return [1, 3, 4]

[LeetCode]103.Binary Tree Zigzag Level Order Traversal

[题目] Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to right, then right to left for the next level and alternate between). For example: Given binary tree {3,9,20,#,#,15,7}, 3 / \ 9 20 / \ 15 7 retur

[LeetCode]102.Binary Tree Level Order Traversal

[题目] Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level). For example: Given binary tree {3,9,20,#,#,15,7}, 3 / \ 9 20 / \ 15 7 return its level order traversal as: [ [3], [9,20], [15,7

LeetCode: Balanced Binary Tree 平衡二叉树

判定一棵二叉树是不是二叉平衡树. 链接:https://oj.leetcode.com/problems/balanced-binary-tree/ 题目描述: Given a binary tree, determine if it is height-balanced.For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtree

[LeetCode]--257. Binary Tree Paths

Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 / \ 2 3 \ 5 All root-to-leaf paths are: ["1->2->5", "1->3"] Credits: Special thanks to @jianchao.li.fighter for adding this pr

[LeetCode]145.Binary Tree Postorder Traversal

[题目] Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary tree {1,#,2,3}, 1 \ 2 / 3 return [3,2,1]. Note: Recursive solution is trivial, could you do it iteratively? [代码] /*******************************