/**
*
* 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. 支付宝红包口令自动复制到剪贴板脚本js,安卓,IOS通用版

    有客户找到涛舅舅,要求开发一个可以自动支付宝红包口令的js脚本,经过大量探索和优化,目前此脚本功能已经测试成功! 预期效果: 只要来访用户在当前网页的任意位置点击一下,支付宝红包口令即可复制到用户手机 ...

  2. git cannot lock ref

    参考博客:https://blog.csdn.net/lindexi_gd/article/details/79213042 错误原文: cannot lock ref ‘refs/remotes/o ...

  3. 编程菜鸟的日记-初学尝试编程-C++ Primer Plus 第6章编程练习8

    #include <iostream> #include <fstream> #include <cstdlib> const int SIZE=20; using ...

  4. 无法运行 vue-manage-system@3.1.0 dev: `webpack-dev-server --inline --progress --

    一个项目的变大好多人开发,难免会有很多的冲突.每次跟新代码都要一个坑一个坑的解决的.这次遇到这个坑好大.急死了.... 百度了好多说占用端口,试了好几遍不行.最终还是要去查原因的....经过了几个小时 ...

  5. jmeter学习笔记(一)

    1.添加JSON Path Extractor >>下载地址:http://jmeter-plugins.org/downloads/all/,下载 JMeterPlugins-Extra ...

  6. apicloud实现各种自定义弹层组件

  7. JAVA---MYSQL 基本知识点 第二部分

    增删改查 (CRUD):   数据库  , 表  , 记录  ;   约束 ; 主键约束 :primary key  如果是int类型 可以使用 自动增长型  auto_increment; 唯一约束 ...

  8. service注入失败

    每一个service都需要一个注解

  9. 机器学习之正则化(Regularization)

    1. The Problem of Overfitting 1 还是来看预测房价的这个例子,我们先对该数据做线性回归,也就是左边第一张图. 如果这么做,我们可以获得拟合数据的这样一条直线,但是,实际上 ...

  10. gps 经纬度 转换实际距离

    <!doctype html> <html lang="zh-CN"> <head> <meta charset="utf-8& ...