***Binary Tree Maximum Path Sum

2024-06-18 17:48
文章标签 binary tree maximum path sum

本文主要是介绍***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 does not need to go through the root.

For example:
Given the below binary tree,

       1/ \2   3

Return 6.

分析:

不太明白什么意思!不会做!!!

http://www.programcreek.com/2013/02/leetcode-binary-tree-maximum-path-sum-java/

1) Recursively solve this problem
2) Get largest left sum and right sum
2) Compare to the stored maximum


参考代码:

/*** 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) {int max[] = new int[1]; max[0] = Integer.MIN_VALUE;calculateSum(root, max);return max[0];}public int calculateSum(TreeNode root, int[] max) {if (root == null)return 0;int left = calculateSum(root.left, max);int right = calculateSum(root.right, max);int current = Math.max(root.val, Math.max(root.val + left, root.val + right));max[0] = Math.max(max[0], Math.max(current, left + root.val + right));return current;}
}

版本2:

http://blog.csdn.net/linhuanmars/article/details/22969069

/*** 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) {if(root==null)return 0;ArrayList<Integer> res = new ArrayList<Integer>();res.add(Integer.MIN_VALUE);helper(root,res);return res.get(0);}private int helper(TreeNode root, ArrayList<Integer> res){if(root == null)return 0;int left = helper(root.left, res);int right = helper(root.right, res);int cur = root.val + (left>0?left:0)+(right>0?right:0);if(cur>res.get(0))res.set(0,cur);return root.val+Math.max(left, Math.max(right,0));}
}


这篇关于***Binary Tree Maximum Path Sum的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1072692

相关文章

Go语言中的go.mod与go.sum

问题1:什么是go.mod以及它是用来解决什么问题的? go mod 是 Go 语言引入的包管理工具,用于解决 Go 语言项目在依赖管理方面的问题。 传统上,若不使用go mod,则需要要通过GOPATH来管理依赖,而这种方式存在一些问题: 如1. 包管理依赖不明确 2. 依赖库的版本管理 3. 需要手动管理同步依赖的复杂性等 而go mod可以帮助开发者在项目中明确管理依赖的版

Android自定义系列——9.Path详细用法

rXxx方法 rXxx方法的坐标使用的是相对位置(基于当前点的位移),而之前方法的坐标是绝对位置(基于当前坐标系的坐标)。 Path path = new Path();path.moveTo(100,100);path.lineTo(100,200);canvas.drawPath(path,mDeafultPaint); 在这个例子中,先移动点到坐标(100,100)处,之后再连接

Android自定义系列——8.Path之贝塞尔曲线

贝塞尔曲线能干什么 贝塞尔曲线作用十分广泛,简单举几个的栗子: QQ小红点拖拽效果一些炫酷的下拉刷新控件阅读软件的翻书效果一些平滑的折线图的制作很多炫酷的动画效果 理解贝塞尔曲线的原理 一阶曲线原理: 一阶曲线是没有控制点的,仅有两个数据点(A 和 B),最终动态过程如下: (本文中贝塞尔曲线相关的动态演示图片来自维基百科)。一阶曲线其实就是前面讲解过的lineTo。 二阶曲线

Android自定义系列——7.Path之基本操作

Path常用方法表 为了兼容性(偷懒) 本表格中去除了部分API21(即安卓版本5.0)以上才添加的方法。 作用相关方法备注移动起点moveTo移动下一次操作的起点位置设置终点setLastPoint重置当前path中最后一个点位置,如果在绘制之前调用,效果和moveTo相同连接直线lineTo添加上一个点到当前点之间的直线到Path闭合路径close连接第一个点连接到最后一个点,形成一个闭合

玩转Web之easyui(二)-----easy ui 异步加载生成树节点(Tree),点击树生成tab(选项卡)

关于easy ui 异步加载生成树及点击树生成选项卡,这里直接给出代码,重点部分代码中均有注释 前台: $('#tree').tree({ url: '../servlet/School_Tree?id=-1', //向后台传送id,获取根节点lines:true,onBeforeExpand:function(node,param){ $('#tree').tree('options'

从PATH说起的shell命令行替换

许久之前,师弟问了我一个问题,为什么shell中添加环境变量的写法是下面这种方式 PATH=~/.lib:$PATH; export PATH 而下面这种会报错呢? $PATH=~/.lib:$PATH; export PATH 当时我的回答是,"shell就是这样子规定的呀"。 回答的同时,也突然间发现有些自己感觉很熟悉的概念,原来自己并没有那么清楚,因此这一篇讲讲shell的命令行

Java环境变量配置中有关JAVA_HOME,path,Classpath含义的讲解

一:Path变量 Path变量是操作系统的,用以找寻相关命令的。例如ping这个命令,你能在控制行里打ping 127.0.0.1而有程序执行并正确返回结果,是因为Path变量包含C:\Windows\System32。你可以在Path中把C:\Windows\System32去掉,再使用ping命令,就会提示找不到ping命令。 这就像你在你的办公桌上工作,需要用到各种工具,如钢笔,

【C++PCL】点云处理Kd-tree原理

作者:迅卓科技 简介:本人从事过多项点云项目,并且负责的项目均已得到好评! 公众号:迅卓科技,一个可以让您可以学习点云的好地方 重点:每个模块都有参数如何调试的讲解,即调试某个参数对结果的影响是什么,大家有问题可以评论哈,如果文章有错误的地方,欢迎来指出错误的地方。 目录         1.原理介绍 1.原理介绍         kd-tree是散乱点云的一种储存结构,它是一种

poj 1564 Sum It Up -- DFS 递归

题意:给一个数 t ,以及 n 个数,求 n 个数中的几个数加起来的和为 t 的情况有多少种。 注意:题目要求相同的组合方式不能出现2次,即 “3 4 1 1 1 1 ” 的结果为:“1+1+1”。 思路:一个  for  循环遍历一遍,每个 i 表示以当前数为起点开始一次DFS递归,当所遍历的和为 t 时,输出该组合方式,如果大于 t 则返回,小于则往下递归。以二维数组保存已经输出过的数据,

[leetcode] 107. Binary Tree Level Order Traversal II

Binary Tree Level Order Traversal II 描述 Given a binary tree, return the bottom-up level order traversal of its nodes’ values. (ie, from left to right, level by level from leaf to root). For example