leetcode — binary-tree-maximum-path-sum
/**
*
* Source : https://oj.leetcode.com/problems/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.
*/
public class BinaryTreeMaximumPathSum {
/**
* 求出遍历树节点的时候最大和,
* 可以从任何地方开始遍历,可以在任何地方结束
*
* 分析得出有两种遍历方式:
* 1. 从根节点到叶子节点之间的一段,root-leaf path中的一段
* 2. 两个节点之间经过最小公共祖先节点的path
*
* 分别对于两种情况求出最大值,然后比较求出较大的一个
*
* 定义:
* sum1为第一种情况下,一个节点可能出现的最大值
* sum1(root) = max(max(sum1(root.left), 0), max(sum1(root.right), 0)) + root.value
*
* sum2为第二种情况下,一个节点可能出现的最大值
* sum2(root) = max(sum1(root.left), 0) + max(sum1(root.right), 0) + root.value
*
*
* @param root
* @return
*/
public int maxPathSum (TreeNode root) {
MaxSumHolder holder = new MaxSumHolder();
holder.value = Integer.MIN_VALUE;
return recursion(root, holder);
}
public int recursion (TreeNode root, MaxSumHolder holder) {
if (root == null) {
return 0;
}
int sum1Left = 0;
int sum1Right = 0;
if (root.leftChild != null) {
sum1Left = Math.max(recursion(root.leftChild, holder), 0);
}
if (root.rightChild != null) {
sum1Right = Math.max(recursion(root.rightChild, holder), 0);
}
int sum1 = Math.max(sum1Left, sum1Right) + root.value;
int sum2 = sum1Left + sum1Right + root.value;
holder.value = Math.max(holder.value, Math.max(sum1, sum2));
return holder.value;
}
private class MaxSumHolder {
int value;
}
public TreeNode createTree (char[] treeArr) {
TreeNode[] tree = new TreeNode[treeArr.length];
for (int i = 0; i < treeArr.length; i++) {
if (treeArr[i] == '#') {
tree[i] = null;
continue;
}
tree[i] = new TreeNode(treeArr[i]-'0');
}
int pos = 0;
for (int i = 0; i < treeArr.length && pos < treeArr.length-1; i++) {
if (tree[i] != null) {
tree[i].leftChild = tree[++pos];
if (pos < treeArr.length-1) {
tree[i].rightChild = tree[++pos];
}
}
}
return tree[0];
}
private class TreeNode {
TreeNode leftChild;
TreeNode rightChild;
int value;
public TreeNode(int value) {
this.value = value;
}
public TreeNode() {
}
}
public static void main(String[] args) {
BinaryTreeMaximumPathSum maximumPathSum = new BinaryTreeMaximumPathSum();
char[] arr = new char[]{'1','2','3'};
System.out.println( maximumPathSum.maxPathSum(maximumPathSum.createTree(arr)) + "----6");
}
}
leetcode — binary-tree-maximum-path-sum的更多相关文章
- [leetcode]Binary Tree Maximum Path Sum
Binary Tree Maximum Path Sum Given a binary tree, find the maximum path sum. The path may start and ...
- LeetCode: Binary Tree Maximum Path Sum 解题报告
Binary Tree Maximum Path SumGiven a binary tree, find the maximum path sum. The path may start and e ...
- 二叉树系列 - 二叉树里的最长路径 例 [LeetCode] Binary Tree Maximum Path Sum
题目: Binary Tree Maximum Path Sum Given a binary tree, find the maximum path sum. The path may start ...
- [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
1.题目说明 Given a binary tree, find the maximum path sum. The path may start and end at any node in t ...
- C++ leetcode Binary Tree Maximum Path Sum
偶然在面试题里面看到这个题所以就在Leetcode上找了一下,不过Leetcode上的比较简单一点. 题目: Given a binary tree, find the maximum path su ...
- [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 @ Python
原题地址:https://oj.leetcode.com/problems/binary-tree-maximum-path-sum/ 题意: Given a binary tree, find th ...
- [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 二叉树最大路径和(DFS)
题意:给一棵二叉树,要求找出任意两个节点(也可以只是一个点)的最大路径和,至少1个节点,返回路径和.(点权有负的.) 思路:DFS解决,返回值是,经过从某后代节点上来到当前节点且路径和最大的值.要注意 ...
随机推荐
- dnmp(docker的lnmp)安装WordPress之后图片上传问题 问题:图片上传大小问题解决和 报错413 Request Entity Too Large
首先是提示超过图片尺寸和大小, 最后发现都是图片大小的问题, 需要修改php的最大上传size 修改之后查看php配置 已经生效 但是还是报错, 提示返回不是合法的json, 查看控制台, 报错 ...
- VMware Workstation安装Red hat7.0联网问题总结
1.在red hat7当中iconfig命令是被取消了的,开发者用ip addr命令取代了ifconfig命令. 当然也是可以用ifconfig命令的 (前提是你安装的linux是可以联网的): 1) ...
- box-shadow 画叮当猫
值 描述 h-shadow 必需.水平阴影的位置.允许负值 v-shadow 必需.垂直阴影的位置.允许负值 blur 可选.模糊距离 spread 可选.阴影的尺寸 color 可选.阴影的颜色.请 ...
- ssh 连接失败 sz rz 安装
sz 下载命令, rz上传命令的安装 sudo apt-get install lrzsz 1. 检查sshd服务的状态以及端口是否正常, 如下为正常状态 sudo netstat -nlp | gr ...
- Nginx如何对日志文件进行配置?
在我们日常工作开发中,对调试bug最重要的手段就是查看日志和断点调试了. 今天我们来说日志文件,Nginx的日志文件一般保存的是访问日志和错误日志. 1. 用来log_format指令设置日志格式 l ...
- yarn的工作原理
1.YARN 是什么? 从业界使用分布式系统的变化趋势和 hadoop 框架的长远发展来看,MapReduce的 JobTracker/TaskTracker 机制需要大规模的调整来修复它在可扩展性, ...
- Servlet 上传下载文件
上传文件 1)在表单中使用表单元素 <input type=“file” />,浏览器在解析表单时,会自动生成一个输入框和一个按钮 2)表单需要上传文件时,需指定表单 enctype 的值 ...
- Winform将一个窗体显示在另一个窗体中
private void ShowForm(Form Indexform) { Form1 form1 = new Form1(); form1 .TopLevel = false; form1 .P ...
- python Tags 母板 组件 静态文件相关 自定义simpletag inclusion_tag
一.Tags(一)for 1.基本用法 <ul> {% for user in user_list %} <li>{{ user.name }}</li> {% e ...
- FFmpeg 学习(六):FFmpeg 核心模块 libavformat 与 libavcodec 分析
一.libavformat介绍 libavformat的主要组成与层次调用关系如下图: AVFromatContext是API层直接接触到的结构体,它会进行格式的封装和解封装,它的数据部分由底层提供, ...