二叉树中的最大路径和
来源:互联网 发布:家庭理财记账软件 编辑:程序博客网 时间:2024/05/22 03:24
private class ResultType { // singlePath: 从root往下走到任意点的最大路径,这条路径可以不包含任何点 // maxPath: 从树中任意到任意点的最大路径,这条路径至少包含一个点 int signlePath, maxPath; ResultType(int signlePath, int maxPath) { this.signlePath = signlePath; this.maxPath = maxPath; } } public int maxPathSum(TreeNode root) { // write your code here return helper(root).maxPath; } public ResultType helper(TreeNode root){ if (root == null){ return new ResultType(0, Integer.MIN_VALUE); } ResultType left = helper(root.left); ResultType right = helper(root.right); int signlePath = Math.max(left.signlePath, right.signlePath) + root.val; signlePath = Math.max(signlePath, 0); int maxPath = Math.max(left.maxPath, right.maxPath); /*maxPath = Math.max(maxPath, left.siglePath + right.siglePath + root.val);*/ maxPath = Math.max(maxPath, left.signlePath + right.signlePath + root.val); return new ResultType(signlePath, maxPath); }}
阅读全文
0 0
- LintCode-二叉树中的最大路径和
- 二叉树中的最大路径和
- 二叉树中的最大路径和
- 题目:二叉树中的最大路径和
- 二叉树中的最大路径和
- 二叉树中的最大路径和
- lintcode,二叉树中的最大路径和
- 二叉树中的最大路径和
- 二叉树中的最大路径和
- 二叉树中的最大路径和-LintCode
- LintCode 二叉树中的最大路径和
- lintcode--二叉树中的最大路径和
- 二叉树中的最大路径和-LintCode
- 二叉树中的最大路径和
- 二叉树最大路径和
- 二叉树最大路径和
- 二叉树最大路径和
- lintcode-二叉树中的最大路径和-94
- IDEA 调试技巧
- Android环境搭建
- 树状数组
- poj2689 素数距离(埃氏筛法分块筛+暴力枚举)
- HZDU 1008 Elevator
- 二叉树中的最大路径和
- zedboard qspi flash启动时,为什么program flash的加载速度很慢
- Android Studio中使用lambda表达式
- 图像特征提取
- C++/MFC-进程优先级
- 每天一个linux命令(50):crontab命令
- PhpExcel中文帮助手册|PhpExcel使用方法
- supersu 分类挂载命名空间 影响
- 【Python学习】Python解决汉诺塔问题