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.For example: Given the below binary tree,1/ \2 3 Return 6.
难度:75.
参见了他人的思路,这道题是求树的路径和的题目,不过和平常不同的是这里的路径不仅可以从根到某一个结点,而且路径可以从左子树某一个结点,然后到达右子树的结点,就像题目中所说的可以起始和终结于任何结点。函数的返回值定义为以自己为根的一条从根到叶子结点的最长路径,这个返回值是为了提供给它的父结点计算自身的最长路径用。这样一来,一个结点自身的最长路径就是它的左子树返回值(如果大于0的话),加上右子树的返回值(如果大于0的话),再加上自己的值。在过程中求得当前最长路径时比较一下是不是目前最长的,如果是则更新。算法的本质还是一次树的遍历,所以复杂度是O(n)。而空间上仍然是栈大小O(logn)。注意这里path的存值方式是个问题,如果用一个integer变量代入recursion的话,函数无法对实参造成改变,所以用了一个对象ArrayList<Integer> res的第一个元素来存最大的path值
1 /** 2 * Definition for binary tree 3 * public class TreeNode { 4 * int val; 5 * TreeNode left; 6 * TreeNode right; 7 * TreeNode(int x) { val = x; } 8 * } 9 */ 10 public class Solution { 11 public int maxPathSum(TreeNode root) { 12 if (root == null) return 0; 13 ArrayList<Integer> res = new ArrayList<Integer>(); 14 res.add(Integer.MIN_VALUE); 15 FindMaxSum(root, res); 16 return res.get(0); 17 } 18 19 public int FindMaxSum(TreeNode root, ArrayList<Integer> res) { 20 if (root == null) { 21 return 0; 22 } 23 int leftsum = FindMaxSum(root.left, res); 24 int rightsum = FindMaxSum(root.right, res); 25 int maxsum = root.val + (leftsum>0? leftsum : 0) + (rightsum>0? rightsum : 0); 26 if (maxsum > res.get(0)) res.set(0, maxsum); 27 return root.val + Math.max(leftsum, Math.max(rightsum, 0)); 28 } 29 }
Leetcode: Binary Tree Maximum Path Sum相关推荐
- 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. ...
- [Leetcode] Binary Tree Maximum Path Sum
这是LeetCode上的一道题目,需要求二叉树中两点路径的最大和.原题是 https://oj.leetcode.com/problems/binary-tree-maximum-path-sum/ ...
- 【重点】LeetCode 124. Binary Tree Maximum Path Sum
LeetCode 124. Binary Tree Maximum Path Sum 参考链接:http://zxi.mytechroad.com/blog/tree/leetcode-124-bin ...
- 【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 ...
- Binary Tree Maximum Path Sum
Binary Tree Maximum Path Sum 题目链接:https://leetcode.com/problems... dfs对每个node,查一下包含这个node的最大路径值. /** ...
- 124 Binary Tree Maximum Path Sum
题目: 124 Binary Tree Maximum Path Sum 这道题就是分别算出左子树和右子树的可能最大和,然后对Path的值进行更新即可 class Solution:def __ini ...
- leetcode @python 124. Binary Tree Maximum Path Sum
题目链接 https://leetcode.com/problems/binary-tree-maximum-path-sum/ 题目原文 Given a binary tree, find the ...
- LeetCode 124. Binary Tree Maximum Path Sum
原题 求二叉树的最大路径和 Given a binary tree, find the maximum path sum. For this problem, a path is defined as ...
- 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 ...
最新文章
- Java项目:嘟嘟二手书商城系统(java+JSP+Springboot+maven+mysql+ThymeLeaf+FTP)
- go同一个目录下的go文件里面不能有多个package
- python判断相同,Python判断两个文件是否相同与两个文本进行相同项筛选的方法
- LeetCode 1289. 下降路径最小和 II(DP)
- python漂亮的螺旋_CANVAS 各种螺旋画出来的漂亮图案
- css设置字体颜色、文本对齐方式、首行缩进、文本装饰、列表样式、鼠标样式、禁止文本域拖拽、轮廓线、块级元素对齐方式、文字溢出设置
- Objective-C 日记③ 字符串
- 安卓系统通过陀螺仪计算当前坐标(焦点位置)
- viper4android最新,ViPER4Android FX音效驱动下载-ViPER4Android音效驱动 v2.4.0.1 正式版_手机乐园...
- 智能戒指,一个新鲜智能穿戴产物
- 哨兵卫星影像数据Sentinel-1基本参数
- 2019上半年个人成长复盘
- 5G 核心网 Quality of Service Model (QoS Model)
- Load和Initialize的区别和使用
- JavaSwing设置字体样式
- Sqoop 使用详解
- 算法导论 之 红黑树 - 添加[C语言]
- 你“云”了吗?华为云带你看懂云桌面
- python midi_Python | 读取 midi 文件
- abd连接手机的三种方法
热门文章
- 业务需求、客户需求与功能需求
- 外贸常用术语_外贸干货!做外贸常用的贸易术语,几个妙招教你快速记住!
- python 欧氏距离_如何用NumPy计算欧氏距离?
- mysql+主从复制重做_XtraBackup实现MySQL不停机重做主从复制
- ai包装插件_找AI插件很费劲,一次给你66款AI插件合集!每一款都是设计师常用...
- 信息系统项目管理师优秀论文:项目整体管理
- 《系统集成项目管理工程师》必背100个知识点-58沟通方式
- 笔记-项目质量管理-编制质量管理计划的工具与技术
- 创业——不可忽视的创业计划书!!!
- CentOS7中多台服务器配置SSH免密钥登录