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解决,返回值是,经过从某后代节点上来到当前节点且路径和最大的值.要注意 ...
随机推荐
- Do-Now—团队冲刺博客一(领航篇)
Do Now -- 团队冲刺博客一(领航篇) 团队博客总目录:团队作业第一周 团队作业第二周 Do Now -- 团队冲刺博客一 领航目标 ① 各个成员在 Alpha 阶段认领的任务 ② 明日各个成员 ...
- mysql远程访问以及权限设置
前几天看见有人问数据库怎么远程访问,当时想不是很简单么,以前就用Navicat连接过服务器数据库,一连就连上了.然后自己试了试...不行.什么情况??!!!报的错误是100061,网上一搜,是权限问题 ...
- 《SpringMVC从入门到放肆》十四、SpringMVC分组数据校验
上一篇我们学习了数据校验,但是在实际项目中,还是有些不够灵活,今天我们就来继续学习一种更灵活的数据校验方法——分组数据校验. 一.什么是分组校验 校验规则是定义在实体中的,而同一个实体可以被多个Con ...
- 机器学习(七)EM算法、GMM
一.GMM算法 EM算法实在是难以介绍清楚,因此我们用EM算法的一个特例GMM算法作为引入. 1.GMM算法问题描述 GMM模型称为混合高斯分布,顾名思义,它是由几组分别符合不同参数的高斯分布的数据混 ...
- data.table包使用应该注意的一些细节
fread中nThread 参数的使用 注意默认nThread=getDTthreads(),即使用所有能用的核心,但并不是核心用的越多越好,本人亲自测试的情况下,其实单核具有较强的性能,只有在数 ...
- 巧用PHP中__get()魔术方法
PHP中的魔术方法有很多,这些魔术方法可以让PHP脚本在某些特定的情况下自动调用.比如 __construct() 每次实例化一个类都会先调用该方法进行初始化.这里我们讲一下__get() 魔术方法的 ...
- web全套资料 干货满满 各种文章详解
sql注入l MySqlMySQL False注入及技巧总结MySQL 注入攻击与防御sql注入学习总结SQL注入防御与绕过的几种姿势MySQL偏门技巧mysql注入可报错时爆表名.字段名.库名高级S ...
- [安卓] 20、基于蓝牙BLE的广播包高频快速搜索
前言: 之前介绍过很多蓝牙beacon.搜索.连接.通讯的文章.不过最近我发现:之前写的蓝牙广播包搜索的工程,搜索频率太慢,而且不能一直保持搜索状态.因此,这里探讨下高频蓝牙广播包扫描 -- 蓝牙BL ...
- 【二代示波器教程】第14章 uCOS-III操作系统版本二代示波器实现
第14章 uCOS-III操作系统版本二代示波器实现 本章教程为大家讲解uCOS-III操作系统版本的二代示波器实现.主要讲解RTOS设计框架,即各个任务实现的功能,任务间的通信方案选择,任 ...
- Java 三种方式实现接口校验
方法一:AOP 代码如下定义一个权限注解 package com.thinkgem.jeesite.common.annotation; import java.lang.annotation.Ele ...