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

資訊專(zhuān)欄INFORMATION COLUMN

[Leetcode-Tree]Binary Tree Maximum Path Sum

caige / 443人閱讀

摘要:但是本題的難點(diǎn)在于,使用遞歸實(shí)現(xiàn),但是前面的第四種情況不能作為遞歸函數(shù)的返回值,所以我們需要定義兩個(gè)值,代表單邊路徑的最大值,用于遞歸用于和回路的較大值。

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 to go through the root.

For example:
Given the below binary tree,

   1
  / 
 2   3

Return 6.

1.解題思路

分析對(duì)于每一個(gè)根節(jié)點(diǎn),可能的路徑和有:左子樹(shù)的最大路徑和+該節(jié)點(diǎn)val,右子樹(shù)的最大路徑和+該節(jié)點(diǎn)val,只取該節(jié)點(diǎn)val,左子樹(shù)的最大路徑和+該節(jié)點(diǎn)val+右子樹(shù)的最大路徑和,我們需要取四個(gè)中最大的一個(gè)即可。但是本題的難點(diǎn)在于,使用遞歸實(shí)現(xiàn),但是前面的第四種情況不能作為遞歸函數(shù)的返回值,所以我們需要定義兩個(gè)max值,singleMax代表單邊路徑的最大值,用于遞歸;curMax用于singleMax和回路Max的較大值。

2.代碼

public class Solution {
    int max=Integer.MIN_VALUE;
    public int maxPathSum(TreeNode root) {
        helper(root);
        return max;
    }
    private int helper(TreeNode root){
        if(root==null) return 0;
        int leftsum=helper(root.left);
        int rightsum=helper(root.right);
        int singlemax=Math.max(Math.max(leftsum+root.val,rightsum+root.val),root.val);
        int curmax =Math.max(singlemax,leftsum+root.val+rightsum);
        max=Math.max(max,curmax);
        return singlemax;
    }
}

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

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

相關(guān)文章

  • [Leetcode-Tree]Maximum / Minimum Depth of Binary T

    摘要:解題思路用遞歸實(shí)現(xiàn)很簡(jiǎn)單,對(duì)于每個(gè)根節(jié)點(diǎn),最大深度就等于左子樹(shù)的最大深度和右子樹(shù)的最大深度的較大值。解題思路本題的注意點(diǎn)在于如果某個(gè)根節(jié)點(diǎn)有一邊的子樹(shù)為空,那么它的深度就等于另一邊不為空的子樹(shù)的深度,其他的邏輯與上一題相同。 Maximum Depth of Binary TreeGiven a binary tree, find its maximum depth. The maxi...

    Thanatos 評(píng)論0 收藏0
  • [Leetcode-Tree] Path Sum I II III

    摘要:解題思路利用遞歸,對(duì)于每個(gè)根節(jié)點(diǎn),只要左子樹(shù)和右子樹(shù)中有一個(gè)滿足,就返回每次訪問(wèn)一個(gè)節(jié)點(diǎn),就將該節(jié)點(diǎn)的作為新的進(jìn)行下一層的判斷。代碼解題思路本題的不同點(diǎn)是可以不從開(kāi)始,不到結(jié)束。代碼當(dāng)前節(jié)點(diǎn)開(kāi)始當(dāng)前節(jié)點(diǎn)左節(jié)點(diǎn)開(kāi)始當(dāng)前節(jié)點(diǎn)右節(jié)點(diǎn)開(kāi)始 Path SumGiven a binary tree and a sum, determine if the tree has a root-to-lea...

    notebin 評(píng)論0 收藏0
  • [Leetcode-Tree] Sum Root to Leaf Numbers

    摘要:解題思路本題要求所有從根結(jié)點(diǎn)到葉子節(jié)點(diǎn)的路徑和,我們用遞歸實(shí)現(xiàn)。結(jié)束條件當(dāng)遇到葉子節(jié)點(diǎn)時(shí),直接結(jié)束,返回計(jì)算好的如果遇到空節(jié)點(diǎn),則返回?cái)?shù)值。 Sum Root to Leaf NumbersGiven a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a numbe...

    BigNerdCoding 評(píng)論0 收藏0
  • [Leetcode-Tree]Sum of Left Leaves

    摘要:解題思路這個(gè)題目其實(shí)就是基于先序遍歷,用遞歸和非遞歸思想都可以。遞歸求所有左葉子節(jié)點(diǎn)的和,我們可以將其分解為左子樹(shù)的左葉子和右子樹(shù)的左葉子和遞歸結(jié)束條件找到左葉子節(jié)點(diǎn),就可以返回該節(jié)點(diǎn)的。代碼非遞歸判斷是否為左葉子節(jié)點(diǎn)遞歸 Sum of Left LeavesFind the sum of all left leaves in a given binary tree. Example:...

    ashe 評(píng)論0 收藏0
  • 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

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

0條評(píng)論

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