每天刷一道牛客题霸-第21天- 二叉树最大路径和

题目

https://www.nowcoder.com/practice/da785ea0f64b442488c125b441a4ba4a?tpId=190&&tqId=35180&rp=1&ru=/ta/job-code-high-rd&qru=/ta/job-code-high-rd/question-ranking

import java.util.*;

/*
 * public class TreeNode {
 *   int val = 0;
 *   TreeNode left = null;
 *   TreeNode right = null;
 * }
 */

public class Solution {
    /**
     * 
     * @param root TreeNode类 
     * @return int整型
     */
    public static int maxValue;

    public int maxPathSum (TreeNode root) {
        // write code here
        maxValue = Integer.MIN_VALUE;
        getMaxPathSum(root);
        return maxValue;
    }
    public int getMaxPathSum(TreeNode root){
        if(root != null){
            int maxLeftValue = Math.max(0,getMaxPathSum(root.left));
            int maxRightValue = Math.max(0,getMaxPathSum(root.right));
            maxValue = Math.max(maxValue,maxLeftValue + root.val + maxRightValue);
            return Math.max(maxLeftValue , maxRightValue)+ root.val;
        }
        return 0;
    }
}
#牛客题霸##题解#
全部评论
鸟哥元旦快乐啊
点赞 回复 分享
发布于 2020-12-31 18:03

相关推荐

完美的潜伏者许愿简历...:隐藏信息被你提取出来了,暗示,这就是暗示
点赞 评论 收藏
分享
05-26 22:25
门头沟学院 Java
Java小肖:不会是想叫你过去把你打一顿吧,哈哈哈
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
07-08 10:39
一个证都没 我能填什么
程序员小白条:别人有,你为什么没有,还是这个道理,社会就是比较,竞争,淘汰,你要安逸,那么就要做好淘汰的准备
点赞 评论 收藏
分享
评论
2
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务