题目:

Given a non-empty binary tree, find the maximum path sum.

For this problem, a path is defined as any sequence of nodes from some starting node to any node in the tree along the parent-child connections. The path must contain at least one node and does not need to go through the root.

Example 1:

Input: [1,2,3]

       1
/ \
2 3 Output: 6

Example 2:

Input: [-10,9,20,null,null,15,7]

   -10
   / \
  9  20
    /  \
   15   7 Output: 42

分析:

给定一颗二叉树,求其最大路径,路径就是从任意节点出发,到达任意节点的序列,注意路径至少要有一个节点。

这道题和下面两道题做法相同,都是求二叉树的路径问题,可以先回顾下面两个问题。

LeetCode 543. Diameter of Binary Tree 二叉树的直径 (C++/Java)

LeetCode 687. Longest Univalue Path 最长同值路径 (C++/Java)

那么这道题还是从根节点递归求解,当前结点的最大路径,是当前节点的值加上左右孩子的最大路径,同时和全局的最大值进行比较,更新最大值,而作为返回值时,需要在左右孩子中选取最大值加上当前结点的值同时和0比较,选取最大值,因为节点存在负数,那么路径为负数显然是不对的,所以对于值为负数的子树我们向上层返回0即可。

程序:

C++

/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int maxPathSum(TreeNode* root) {
if(root == nullptr)
return ;
int res = INT_MIN;
maxPathSum(root, res);
return res;
}
private:
int maxPathSum(TreeNode* root, int& res){
if(root == nullptr)
return ;
int l = maxPathSum(root->left, res);
int r = maxPathSum(root->right, res);
int sum = l + r + root->val;
res = max(sum, res);
return max(max(l, r) + root->val, );
}
};

Java

/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public int maxPathSum(TreeNode root) {
if(root == null)
return 0;
res = Integer.MIN_VALUE;
maxPath(root);
return res;
}
private int res;
private int maxPath(TreeNode root) {
if(root == null)
return 0;
int l = maxPath(root.left);
int r = maxPath(root.right);
int sum = l + r + root.val;
res = Math.max(sum, res);
return Math.max(Math.max(l, r) + root.val, 0);
}
}

LeetCode 124. Binary Tree Maximum Path Sum 二叉树中的最大路径和 (C++/Java)的更多相关文章

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

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

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

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

  5. leetcode 124. Binary Tree Maximum Path Sum

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

  6. leetcode@ [124] Binary Tree Maximum Path Sum (DFS)

    https://leetcode.com/problems/binary-tree-maximum-path-sum/ Given a binary tree, find the maximum pa ...

  7. leetcode 124. Binary Tree Maximum Path Sum ----- java

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

  8. Java for LeetCode 124 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. ...

  9. 【LeetCode】124. 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. liunx命令用到的

    su:切换成root用户 sudo su:普通用户申请root权限 ping命令可以检查linux是否联网 ping www.baidu.com 如图就是联网了 结束ping包括其他linux的指令 ...

  2. GNS3 模拟icmp重定向

    网关实质上是一个网络通向其他网络的IP地址.比如有网络A和网络B,网络A的IP地址范围为“192.168.1.1~192. 168.1.254”,子网掩码为255.255.255.0:网络B的IP地址 ...

  3. String+、intern()、字符串常量池

    字符串连接符 "+"及字符串常量池实验.字符串final属性 结果预览 public class StrTest{ public static void main(String[] ...

  4. NIO 聊天室代码实现

    服务器端 package com.ronnie.nio.groupChat; import java.io.IOException; import java.net.InetSocketAddress ...

  5. 01 DDL(DataDefinitionLanguage)

    注: 语句用 ; 或 \g \G 表示结束 .       建库语句 :         CREATE DATABASE db_name ;          查询有哪些库 :         SHO ...

  6. Fedora Workstation 31众多功能得到改进

    导读 周一,Red Hat的桌面高级经理Christian F.K. Schaller分享了一篇博客文章,概述了Fedora Workstation 31的各种改进和特性.这些包括Wayland的改进 ...

  7. springcloud--zuul(过滤器)

    在zuul添加过滤器 新建类继承ZuulFilter类. public class MyFilter extends ZuulFilter{ //是否需要过滤 @Override public boo ...

  8. Windows 10中使用VirtualBox

    新版Windows 10或者安装了新的更新以后,VirtualBox虚拟机就不能用了. 原因是WIndows10里面有个叫Virtualization-base security的安全机制打开了. 关 ...

  9. 抓取屏幕并压缩生成HBITMAP

    HBITMAP GetScreenBmp() { HWND hwnd = ::GetDesktopWindow(); HDC hsrc = ::GetDC(hwnd); HDC hmemdc = :: ...

  10. Day4-T2

    原题目 某大厦共有 N 层,现在知道共有 K 个请求要上下电梯:下面告诉你每个请求乘电梯的出发层次和结 束层次.请你求出整个电梯的运行过程.假设电梯一开始停在第一层,运行 K 个请求最后回到第一层. ...