成人国产在线小视频_日韩寡妇人妻调教在线播放_色成人www永久在线观看_2018国产精品久久_亚洲欧美高清在线30p_亚洲少妇综合一区_黄色在线播放国产_亚洲另类技巧小说校园_国产主播xx日韩_a级毛片在线免费

資訊專欄INFORMATION COLUMN

Binary Tree Maximum Path Sum

liujs / 2266人閱讀

摘要:分三種情況,從中取最大左子樹的右子樹的跨根節(jié)點方案把節(jié)點的設為整數(shù)最小值以保證為負時至少一個值比較方案和方案左子樹的和右子樹的跨情況

Given a binary tree, find the maximum path sum.

The path may start and end at any node in the tree.

class ResultType{
    int root2any;
    int any2any;
    ResultType(int root2any, int any2any){
        this.root2any = root2any;
        this.any2any = any2any;
    }
}

public class Solution {
    /**
     * @param root: The root of binary tree.
     * @return: An integer.
     */
    public int maxPathSum(TreeNode root) {
        return helper(root).any2any;
    }
    //分三種情況,從中取最大: 左子樹的any2any, 右子樹的any2any,跨根節(jié)點方案
    public ResultType helper(TreeNode root){
        if (root == null){
            //把null節(jié)點的any2any設為整數(shù)最小值以保證root為負時至少return一個值
            return new ResultType(0,Integer.MIN_VALUE);
        }
        //divide 
        ResultType left = helper(root.left);
        ResultType right = helper(root.right);
        
        
        //conquer
        int root2any = Math.max(0, Math.max(left.root2any,right.root2any)) + root.val;
        //比較方案1和方案2(左子樹的any2any和右子樹的any2any)
        int any2any = Math.max(left.any2any,right.any2any);
        //跨root情況
        any2any = Math.max(any2any, Math.max(0,left.root2any) + Math.max(0,right.root2any) + root.val);
        
        return new ResultType(root2any, any2any);
    }
}

文章版權歸作者所有,未經(jīng)允許請勿轉載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉載請注明本文地址:http://systransis.cn/yun/66417.html

相關文章

  • LeetCode[124] Binary Tree Maximum Path Sum

    摘要:復雜度思路對于每一節(jié)點,考慮到這一個節(jié)點為止,所能形成的最大值。,是經(jīng)過這個節(jié)點為止的能形成的最大值的一條支路。 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 se...

    warmcheng 評論0 收藏0
  • [Leetcode-Tree]Binary Tree Maximum Path Sum

    摘要:但是本題的難點在于,使用遞歸實現(xiàn),但是前面的第四種情況不能作為遞歸函數(shù)的返回值,所以我們需要定義兩個值,代表單邊路徑的最大值,用于遞歸用于和回路的較大值。 Binary Tree Maximum Path SumGiven a binary tree, find the maximum path sum. For this problem, a path is defined as a...

    caige 評論0 收藏0
  • [Leetcode] Binary Tree Maximum Path Sum 二叉樹最大路徑和

    摘要:棧迭代復雜度時間空間遞歸??臻g對于二叉樹思路首先我們分析一下對于指定某個節(jié)點為根時,最大的路徑和有可能是哪些情況。代碼連接父節(jié)點的最大路徑是一二四這三種情況的最大值當前節(jié)點的最大路徑是一二三四這四種情況的最大值用當前最大來更新全局最大 Binary Tree Maximum Path Sum Given a binary tree, find the maximum path sum...

    魏憲會 評論0 收藏0
  • leetcode-124-Binary Tree Maximum Path Sum

    摘要:題目描述舉例題目分析找從任意節(jié)點出發(fā)的任意路徑的最大長度。每個都有可能是其他路徑上的,這種情況要,。每個都有可能作為中心,此時要左側之前的路徑最長長度,左側之前的路徑最長長度,此為中心時候的長度將這個分析單元遞歸封裝,即可實現(xiàn)目標。 題目描述: Given a binary tree, find the maximum path sum. For this problem, a p...

    z2xy 評論0 收藏0
  • leetcode124. Binary Tree Maximum Path Sum

    摘要:題目要求題目要求從二叉樹中找到任意兩個節(jié)點構成的一條路徑,該路徑上節(jié)點的和為最大。其實在這里我們通過遞歸的方法可以發(fā)現(xiàn)以下幾種場景當前節(jié)點作為起始節(jié)點當前節(jié)點不是起始節(jié)點首先我們以當前節(jié)點作為根節(jié)點,找到可能構成的最大路徑值。 題目要求 Given a binary tree, find the maximum path sum. For this problem, a path i...

    frank_fun 評論0 收藏0
  • [LintCode/LeetCode] Binary Tree Maximum Path Sum

    摘要:調(diào)用函數(shù)更新路徑和的最大值,而函數(shù)本身需要遞歸,返回的是單邊路徑和。所以函數(shù)要返回的是,主函數(shù)中返回的卻是最上一層根節(jié)點處和的較大值,與之前遍歷過所有路徑的最大值之間的最大值。 Problem Given a binary tree, find the maximum path sum. The path may start and end at any node in the tre...

    cnTomato 評論0 收藏0

發(fā)表評論

0條評論

最新活動
閱讀需要支付1元查看
<