问题

  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 

  Return6.

思路

  用递归方法从叶节点开始,将所求最大路径和maxValue设为全局变量,并赋初始值。

  假设递归到节点n,首先计算左子树的最大路径和left,并与0进行比较,若left<0,则left=0。同理求出右子树最大路径和right。

  将maxValue与left+right+root.val比较,把较大值赋于maxValue。

  递归函数的函数值为节点n.val与左右子树中较大值的和。

代码

 /**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public int maxValue = Integer.MIN_VALUE; public int maxPathSum(TreeNode root) {
if(root==null)
return 0;
max(root);
return maxValue;
}
public int max(TreeNode root){
if(root==null)
return 0;
int left = Math.max(0, max(root.left));
int right = Math.max(0, max(root.right));
maxValue = Math.max(maxValue, left+right+root.val);
return Math.max(left, right)+root.val;
}
}

树——binary-tree-maximum-path-sum(二叉树最大路径和)的更多相关文章

  1. [leetcode]124. Binary Tree Maximum Path Sum二叉树最大路径和

    Given a non-empty binary tree, find the maximum path sum. For this problem, a path is defined as any ...

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

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

  3. LeetCode 124. Binary Tree Maximum Path Sum 二叉树中的最大路径和 (C++/Java)

    题目: Given a non-empty binary tree, find the maximum path sum. For this problem, a path is defined as ...

  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

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

  6. leetcode 124. Binary Tree Maximum Path Sum 、543. Diameter of Binary Tree(直径)

    124. Binary Tree Maximum Path Sum https://www.cnblogs.com/grandyang/p/4280120.html 如果你要计算加上当前节点的最大pa ...

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

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

  9. 第四周 Leetcode 124. Binary Tree Maximum Path Sum (HARD)

    124. Binary Tree Maximum Path Sum 题意:给定一个二叉树,每个节点有一个权值,寻找任意一个路径,使得权值和最大,只需返回权值和. 思路:对于每一个节点 首先考虑以这个节 ...

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

随机推荐

  1. USACO2018 DEC(Platinum) (树上乱搞,期望+凸包)

    发现这跟\(Gold\)难度简直天差地别啊.. \(T1\) 传送门 解题思路 这道题还是很可做的,发现题意可以传化成一棵树每次从叶子节点删边,然后有\(m\)条限制,形如\(a\)在\(b\)前面删 ...

  2. All men are brothers

    All men are brothers 牛客多校第九场E 给定n个人,起初互不认识 然后m各阶段 每个阶段有两个人x.y认识 求每个阶段选出四个人互不认识的方式 并查集 #include<bi ...

  3. iOS-KMNavigationBarTransition 框架学习

    最后更新: 2017-06-21 一.文件结构 二.KMSwizzle KMSwizzle主要就一个方法交换的代码 2.1 class_getInstanceMethod() 获取某个类实例的方法, ...

  4. bootstrap插件bootstrap-select使用demo

    插件bootstrap-select官网 : https://developer.snapappointments.com/bootstrap-select/ bootstrap-select插件: ...

  5. 《SQL Server 2012 T-SQL基础》读书笔记 - 1.背景

    几个缩写的全称:Data Definition Language (DDL), Data Manipulation Language (DML), and Data Control Language ...

  6. ngTemplateOutlet递归的问题

    今天尝试通过 ng-template 加 ngTemplateOutlet实现一个递归的菜单.但是遇到一个问题:NullInjectorError: No provider for NzMenuDir ...

  7. ssh连接MAC服务器显示No route to host解决方法

    首先MAC操作系统是10 其它电脑通过ssh访问一台mac服务器时,有时候可以登录进去,有显示显示no route to host 并且通过浏览器访问布署在mac上的jenkins,反应奇慢,后来做了 ...

  8. 鸡肋工具-Oracle建表工具

    为什么叫鸡肋工具呢,因为我们完全可以在pl/sql上直接建立表.索引.同义词.授权.触发器等. 写这个工具目的是因为公司的本地.测试环境开发无权创建表,每次成员建表语句千奇百怪不规范,所以写了这么个工 ...

  9. oracle alter index rebuild offline与online

    oracle index build online与offline测试环境为oracle 11.2.0.4 --sql test SQL> conn test/test )); begin .. ...

  10. 【opencv】opencv图像识别的一些基础的基础函数的使用方法

    import cv2 import numpy as np from matplotlib import pyplot as plt pic_path = "deal_with.png&qu ...