亚洲中字慕日产2020,大陆极品少妇内射AAAAAA,无码av大香线蕉伊人久久,久久精品国产亚洲av麻豆网站

資訊專欄INFORMATION COLUMN

Binary Tree Maximum Path Sum

liujs / 2301人閱讀

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

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é)點(diǎn)方案
    public ResultType helper(TreeNode root){
        if (root == null){
            //把null節(jié)點(diǎn)的any2any設(shè)為整數(shù)最小值以保證root為負(fù)時(shí)至少return一個(gè)值
            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);
    }
}

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

轉(zhuǎn)載請(qǐng)注明本文地址:http://www.ezyhdfw.cn/yun/66417.html

相關(guān)文章

  • LeetCode[124] Binary Tree Maximum Path Sum

    摘要:復(fù)雜度思路對(duì)于每一節(jié)點(diǎn),考慮到這一個(gè)節(jié)點(diǎn)為止,所能形成的最大值。,是經(jīng)過(guò)這個(gè)節(jié)點(diǎn)為止的能形成的最大值的一條支路。 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 評(píng)論0 收藏0
  • [Leetcode-Tree]Binary Tree Maximum Path Sum

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

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

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

    魏憲會(huì) 評(píng)論0 收藏0
  • leetcode-124-Binary Tree Maximum Path Sum

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

    z2xy 評(píng)論0 收藏0
  • leetcode124. Binary Tree Maximum Path Sum

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

    frank_fun 評(píng)論0 收藏0
  • [LintCode/LeetCode] Binary Tree Maximum Path Sum

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

    cnTomato 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

最新活動(dòng)
閱讀需要支付1元查看
<