Binary Tree Maximum Path Sum

题目链接:https://leetcode.com/problems...

dfs对每个node,查一下包含这个node的最大路径值。

/*** Definition for a binary tree node.* public class TreeNode {*     int val;*     TreeNode left;*     TreeNode right;*     TreeNode(int x) { val = x; }* }*/
public class Solution {public int maxPathSum(TreeNode root) {dfs(root);return globalMax;}int globalMax = Integer.MIN_VALUE;private int dfs(TreeNode root) {// base caseif(root == null) return 0;int left = Math.max(0, dfs(root.left));int right = Math.max(0, dfs(root.right));globalMax = Math.max(globalMax, left + right + root.val);return Math.max(left, right) + root.val;}
}

Binary Tree Maximum Path Sum相关推荐

  1. 124 Binary Tree Maximum Path Sum

    题目: 124 Binary Tree Maximum Path Sum 这道题就是分别算出左子树和右子树的可能最大和,然后对Path的值进行更新即可 class Solution:def __ini ...

  2. 【LeetCode】124. Binary Tree Maximum Path Sum

    Binary Tree Maximum Path Sum Given a binary tree, find the maximum path sum. The path may start and ...

  3. 【重点】LeetCode 124. Binary Tree Maximum Path Sum

    LeetCode 124. Binary Tree Maximum Path Sum 参考链接:http://zxi.mytechroad.com/blog/tree/leetcode-124-bin ...

  4. 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. ...

  5. 124. Binary Tree Maximum Path Sum

    题目: Given a binary tree, find the maximum path sum. For this problem, a path is defined as any seque ...

  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.Fo ...

  7. 【Binary Tree Maximum Path Sum】cpp

    题目: Given a binary tree, find the maximum path sum. The path may start and end at any node in the tr ...

  8. [Leetcode] Binary Tree Maximum Path Sum

    这是LeetCode上的一道题目,需要求二叉树中两点路径的最大和.原题是 https://oj.leetcode.com/problems/binary-tree-maximum-path-sum/ ...

  9. leetcode @python 124. Binary Tree Maximum Path Sum

    题目链接 https://leetcode.com/problems/binary-tree-maximum-path-sum/ 题目原文 Given a binary tree, find the ...

最新文章

  1. 技术图文:Python 匿名函数 VS. C# Lambda表达式
  2. AngularJs 基础教程 —— 依赖注入
  3. sql中 set 和select 的区别
  4. python的16.1节课后练习16-2比较希特卡和死亡谷的气温的问题,最后图像为什么出现乱码?
  5. 大数据 vr csdn_VR中的数据可视化如何革命化科学
  6. WEB流程设计器 = jQuery + jsPlumb + Bootstrap
  7. MYSQL 远程访问被限制
  8. 联想Y580黑苹果安装
  9. 使用crash工具分析高通ramdump
  10. java实现手机邮箱格式验证
  11. 华为员工能拿多少钱,揭秘一个真实的华为
  12. 9月【笔耕不辍】勋章活动获奖名单公布
  13. 推荐系统的PMF - 概率矩阵分解和协同过滤
  14. MySQL安装一直卡在starting server
  15. Win10配置adb环境
  16. ffmpeg播放器声音效果2-变速不变调及变调
  17. 一张图认识URI和URL
  18. 泛微在环保行业特色应用:项目、采购、工程、财务数字化管理
  19. Redis:本地客户端连接远程服务器方法
  20. qt制作一个画板_Qt 一个简易画板实现(Graphics View).pdf

热门文章

  1. Windows下用C语言获取进程cpu使用率,内存使用,IO情况
  2. 【微信小程序】scroll-view与Page下拉冲突
  3. PHP convet class to json data
  4. 【转】IOS中Json解析的四种方法
  5. [转]符号和运算符参考 (F#)
  6. 从DataSet导出Txt
  7. -9 逆序输出一个整数的各位数字_计算机基础知识: 信息数字化
  8. debug —— set args调试命令(作为程序运行时的参数)
  9. WebRequest 请求被中止: 请求已被取消。 错误解决方法
  10. Linux 内核完成 urb: 完成回调处理者