leetcode第一刷_Binary Tree Maximum Path Sum

这是道好题。

题目指明了路径的起点和重点是任意的,可以是一个节点可以是包含父节点和左右子树的路径。问题的关键是,这个左右子树返回的不能是整个树的最大和,而只能是包含了这个子树根节点的一条左路径或者右路径。也不知道这么说是否明白,画图说明是在太麻烦了。。

这么说吧,题目虽然对路径的条件没有限制,但是路径还是要求的,如果直接返回左右子树的结果,那么这个结果可能是来自于一个叶子节点,也可能是整个子树,什么都有可能,当这个结果实际上是由不包含子树的根节点计算出来的,那么接到上一层的时候,用了这个结果,而实际的贡献了结果的节点最终没有形成一条路径,因此是错的。还有一种情况,子树的结果用到了这个子树的根节点,但是同时用到了子树的左右孙子树,这也是不行的,因为接到上层,就形成了一个树杈一样的东西,也不是路径。

那么,到底返回什么东西才行呢,有三种选择,第一,返回这棵子树的根节点,这样接到上层可定还是条路径,二,这个根节点和他的左子树形成的左路径,三,这个跟节点和他的右路径,总之必须包含这个跟节点,且不能开叉,这样才有意义。

还有个问题,你会问那如果一个子树要不不要还好呢?没关系啊,我们用的是先序遍历,祖先自动成型的情况早考虑过了,更新最后的结果和子问题的返回可以分开考虑,多么神奇。

ac代码如下,很简洁:

int res;
int tpMaxSum(TreeNode *root){
    if(root == NULL)
        return 0;
    int mval = 0, lval = 0, rval = 0;
    mval = root->val;
    if(root->left)
        lval = tpMaxSum(root->left);
    if(root->right)
        rval = tpMaxSum(root->right);
    if(lval>0)  mval += lval;
    if(rval>0)  mval += rval;
    if(mval>res)    res = mval;
    return max(root->val, max(root->val+lval, root->val+rval));
}   

class Solution {
public:
    int maxPathSum(TreeNode *root) {
        if(root == NULL)
            return 0;
        res = 0xc0000000;
        tpMaxSum(root);
        return res;
    }
};

leetcode第一刷_Binary Tree Maximum Path Sum

时间: 2024-11-05 20:33:15

leetcode第一刷_Binary Tree Maximum Path Sum的相关文章

leetcode第一刷_Binary Tree Inorder Traversal

递归实现当然太简单,也用不着为了ac走这样的捷径吧..非递归实现还挺有意思的. 树的非递归遍历一定要借助栈,相当于把原来编译器做的事情显式的写出来.对于中序遍历,先要訪问最左下的节点,一定是进入循环后,不断的往左下走,走到不能走为止,这时候,能够从栈中弹出訪问的节点,相当于"左根右"过程的"根",然后应该怎么做呢?想一下中序遍历完根节点之后应该干嘛,对,是走到右子树中继续反复这个过程,可是有一点,假设这个节点不包括右子树怎么办?这样的情况下,下一个应该訪问的节点应该

leetcode第一刷_Binary Tree Level Order Traversal II

很简单的题目,在想是不是后面就不要更这么简答的了,大家都会写,没人看啊.层序遍历的基础上,加了保存每一层,加了从下往上输出,就是一个vector和一个stack的问题嘛,无他,但手熟尔. class Solution { public: vector<vector<int> > levelOrderBottom(TreeNode *root) { vector<vector<int> > res; if(root == NULL) return res; q

Leetcode solution 124: Binary Tree Maximum Path Sum

Problem Statement Given a non-empty binary tree, find the maximum path sum. For this problem, a path is defined as any sequence of nodes from some starting node to any node in the tree along the parent-child connections. The path must contain at leas

LeetCode OJ:Binary Tree Maximum Path Sum(二叉树最大路径和)

Given a binary tree, find the maximum path sum. For this problem, a path is defined as any sequence of nodes from some starting node to any node in the tree along the parent-child connections. The path does not need to go through the root. For exampl

leetcode || 124、Binary Tree Maximum Path Sum

problem: 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. Hide Tags Tree Depth-first Search 题意:在一棵二叉树中寻找一条路径,使其和最大 thinking: (1)二叉树寻找一条路径比较

【LeetCode】124. Binary Tree Maximum Path Sum

Given a binary tree, find the maximum path sum. For this problem, a path is defined as any sequence of nodes from some starting node to any node in the tree along the parent-child connections. The path must contain at least one node and does not need

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

[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 [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. [题意] 给定一棵二叉树,找出其中路径和最大的路径,然会返回最大路径和. 本题中的路径不是从根节点到叶子节点这样的传统的路径,而是指的二叉树中任意两个节点之间的联通路径.