/**
*
* 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. MyBatis3系列__04CRUD以及参数处理

    本文将会简单介绍一下MyBatis的CRUD以及结合源码讲解一下MyBatis对参数的处理. 作为一个ORM框架,最基本的使用也就是CRUD了,MyBatis提供了两种方法:xml配置文件和动态注解. ...

  2. vue动态切换组件

    <!DOCTYPE html><html> <head> <meta charset="utf-8"> <title>& ...

  3. iphone 屏蔽系统自动更新,消除设置上的小红点

    苹果ios系统的更新频率大家应该都知道,一般来说1个月就会来次更新.这一点让很多人讨厌.主要原因还是iPhone会自动下载更新包,然后一直不停地提示你是否安装更新,问题是我们还找不到关闭提醒和关闭自动 ...

  4. 记录一种下载https网址中的mp4文件的方法

    需要下载一个网页中的视频, 页面中的视频播放器为 JW player, 通过搜索发现可以下载对应的视频. 1. 使用chrome浏览器分析 网页中的视频地址: F12或者右键-->检查, 在打开 ...

  5. vue中实现图片全屏缩放预览,支持移动端

    # 安装 npm install vue-photo-preview --save # 引入 import preview from 'vue-photo-preview' import 'vue-p ...

  6. 旧项目Makefile 迁移CMake的一种方法:include Makefile

    有些c++旧项目用Makefile,要迁移CMake的时候非常痛苦,有些像static pattern的语法和make自带命令 cmake要重写一套非常的麻烦. 因此这里用trick的方法实现了一种i ...

  7. Gradle 学习一

    参考教程:https://guides.gradle.org/consuming-jvm-libraries/ 安装Gradle 下载地址:https://guides.gradle.org 配置环境 ...

  8. 连接Redis_五种数据格式

    前面我们已经准备成功开启Redis服务,其端口号为6379,接下来我们就看看如何使用C#语言来操作Redis.就如MongoDB一样,要操作Redis服务,自然就需要下载C#的客户端,这里通过Nuge ...

  9. microk8s

    https://microk8s.io/ video guide: sudo snap install microk8s --classic #snap install microk8s --clas ...

  10. Web发展史

    Web 万维网常称为WWW(World Wide Web)发展至今仅30年,英国计算机科学家,蒂姆 伯纳斯 李爵士 提出了 World Wide Web的设计方案,1990年李爵士完成了Web 所有的 ...