/**
*
* 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的更多相关文章

  1. [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 ...

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

  3. 二叉树系列 - 二叉树里的最长路径 例 [LeetCode] Binary Tree Maximum Path Sum

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

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

  6. C++ leetcode Binary Tree Maximum Path Sum

    偶然在面试题里面看到这个题所以就在Leetcode上找了一下,不过Leetcode上的比较简单一点. 题目: Given a binary tree, find the maximum path su ...

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

  8. [leetcode]Binary Tree Maximum Path Sum @ Python

    原题地址:https://oj.leetcode.com/problems/binary-tree-maximum-path-sum/ 题意: Given a binary tree, find th ...

  9. [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. ...

  10. LeetCode Binary Tree Maximum Path Sum 二叉树最大路径和(DFS)

    题意:给一棵二叉树,要求找出任意两个节点(也可以只是一个点)的最大路径和,至少1个节点,返回路径和.(点权有负的.) 思路:DFS解决,返回值是,经过从某后代节点上来到当前节点且路径和最大的值.要注意 ...

随机推荐

  1. itunes 备份导致C盘空间不足问题解决办法

    首先,itunes备份的默认路径是 C:\users\你的用户名\Appdata\Roaming\Apple computer 备份的主要存放文件在C:\Users\David_lu\AppData\ ...

  2. hcna(华为)_Telnet篇

    Telnet提供了一个交互式操作界面,允许终端远程登录到任何可以充当 Telnet服务器的设备.Telnet用户可以像通过Console口本地登录一样对 设备进行操作.远端Telnet服务器和终端之间 ...

  3. 英语知识积累-D01-body+animal

    My body What's your name? Here are lots of children playing. Are they happy or sad? Who's waving at ...

  4. HDU-1423 最长公共上升子序列(LCIS)

    问题描述: 给定两个字符串x, y, 求它们公共子序列s, 满足si < sj ( 0 <= i < j < |s|).要求S的长度是所有条件序列中长度最长的. 做过最长公共子 ...

  5. hadoop2-elasticsearch的安装

    本文主要讲elasticsearch-2.2.1的安装过程. 准备工作: 1.搭建虚拟机 你需要先参考 hadoop2集群环境搭建 把你的虚拟机搭建起来-hadoop环境可以先不用搭建(完成步骤1到步 ...

  6. QT—QTextEdit控件显示日志

    功能:利用QTextEdit开发一个日志显示窗口.没有太多操作,需要实现的是日志自动向上滚动,总体的日志量可以控制在x行(比如300行)以内:其他的应用功能我后面继续添加 #include <Q ...

  7. HBase shell scan 过滤器用法总结

    比较器: 前面例子中的regexstring:2014-11-08.*.binary:\x00\x00\x00\x05,这都是比较器.HBase的filter有四种比较器: (1)二进制比较器:如’b ...

  8. Java语法细节 - synchronized和volatile

    目录 synchronized关键字 关键字volatile synchronized关键字 synchronized关键字锁住方法和this的不同之处: public synchronized vo ...

  9. Red and Black---POJ - 1979

    There is a rectangular room, covered with square tiles. Each tile is colored either red or black. A ...

  10. 将字符串XX,SS以“,”符号进行区分并分别存储在数组中

    public static void main(String[] args) { String str = "EAN_13,1534651"; String strs[] = st ...