[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;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:

    int maxPathSum(TreeNode *root) {
        if(root==NULL)
            return 0;
        set<int> s;
        return recursion(root,root,s);
    }
private:
    int recursion(TreeNode *p,TreeNode *root,set<int> &s){

        int left = 0 ,right=0 ,parent = p->val;
        int max = p->val;
        if(p->left==NULL && p->right==NULL)
            return max;
        if(p->left)
            left = recursion(p->left,root,s);
        if(p->right)
            right = recursion(p->right,root,s);

        if(left>max && p->left)
            s.insert(left);
        if(right>max && p->right)
            s.insert(right);
        if(left+parent>max)
            max = left+parent;
        if(right+parent>max)
            max = right+parent;
        if(right+left+parent>max)
            s.insert(right+left+parent);

        if(p==root && !s.empty()){
            set<int>::iterator iter = s.end();
            iter--;
            return  (max>(*iter)?max:(*iter));
        }
        return max;
    }
};

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

时间: 2024-10-18 23:21:57

[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 解题报告

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. [分析]    需要考虑以上两种情况: 1 左子树或者右子树中存有最大路径和 不能和根节点形成一个路径 2 左子树 右子树 和根节点形成最大路径 [代码] /******

[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——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&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