Leetcode-Bianry 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.

Solution:

/**
 * Definition for binary tree
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */

 //NOTE: Need to consider about negtive number, or ask interviewer about this issue!
 //NOTE2: at every node, we need consider about three cases.
 //1. the path start from some node in the lower level and end at the current node, called singlePath.
 //2. the path from some child node in the left and end at some child node at right, called combinePath.
 //3. the path that does not contain the current node, called lowPath.
 //curNode:
 //singlePath = max(left.singlePath, right.singlePath, curNode.val);
 //combinePath = curNode.val+left.singlePath+right.singlePath;
 //lowPath = max(left.combinePath, left.singlePath, left.lowPath, right.ALLPATH);
 //Return:
 //max(root.singlePath, root.combinePath, root.lowPath);
class PathInfo{
    public int singlePath;
    public int combinePath;
    public int lowPath;
    public int singleNodePath;

    public PathInfo(){
        singlePath = 0;
        combinePath = 0;
        lowPath = 0;
    }
}

public class Solution {
    public int maxPathSum(TreeNode root) {
        PathInfo rootInfo = new PathInfo();
        rootInfo = maxPathSumRecur(root);

        int max = rootInfo.singlePath;
        if (rootInfo.combinePath>max)
            max = rootInfo.combinePath;
        if (rootInfo.lowPath>max)
            max = rootInfo.lowPath;

        return max;
    }

    public PathInfo maxPathSumRecur(TreeNode curNode){
        //If current node is a leaf node
        if (curNode.left==null&&curNode.right==null){
            PathInfo path = new PathInfo();
            path.singlePath = curNode.val;
            path.combinePath = curNode.val;
            path.lowPath = curNode.val;
            return path;
        }

        //If not, then get the PathInfo of its child nodes.
        PathInfo left = null;
        PathInfo right = null;
        PathInfo cur = new PathInfo();
        if (curNode.left!=null)
            left = maxPathSumRecur(curNode.left);
        if (curNode.right!=null)
            right = maxPathSumRecur(curNode.right);

        //Now calculate the PathInfo of current node.
        if (right==null)
            cur.singlePath = curNode.val+left.singlePath;
        else if (left==null)
            cur.singlePath = curNode.val+right.singlePath;
        else {
            if (left.singlePath>right.singlePath)
                cur.singlePath = curNode.val+left.singlePath;
            else
                cur.singlePath = curNode.val+right.singlePath;
        }
        if (cur.singlePath<curNode.val)
            cur.singlePath=curNode.val;

        if (right==null)
            cur.combinePath = curNode.val+left.singlePath;
        else if (left==null)
            cur.combinePath = curNode.val+right.singlePath;
        else
            cur.combinePath = curNode.val+left.singlePath+right.singlePath;

        int max = Integer.MIN_VALUE;
        if (right==null){
            max = left.lowPath;
            if (left.combinePath>max)
                max = left.combinePath;
        } else if (left==null){
            max = right.lowPath;
            if (right.combinePath>max)
                max = right.combinePath;
        } else {
            max = left.lowPath;
            if (left.combinePath>max)
                max = left.combinePath;
            if (right.lowPath>max)
                max = right.lowPath;
            if (right.combinePath>max)
                max = right.combinePath;
        }
        if (max<cur.singlePath)
            max=cur.singlePath;

        cur.lowPath = max;

        return cur;
    }
}

递归求解:对于当前node,计算三种情况的max path sum.

时间: 2024-10-11 17:50:22

Leetcode-Bianry 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

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

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