题意:给一棵二叉树,要求找出任意两个节点(也可以只是一个点)的最大路径和,至少1个节点,返回路径和。(点权有负的。)

思路:DFS解决,返回值是,经过从某后代节点上来到当前节点且路径和最大的值。要注意如果子树传来的如果是负值,是可以同时丢弃的,但至少要将当前节点的val更新答案。

 /**
* 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) {
ans=-;
DFS(root);
return ans;
}
private:
int ans;
int DFS(TreeNode* t) {
if(!t) return ;
int a=max(,DFS(t->left)); //小于0的直接丢弃
int b=max(,DFS(t->right));
ans=max(ans, a+b+t->val); //更新,必须以当前节点为中转
return max(a, b)+t->val; //只能返回一条到叶子的路径
}
};

AC代码

LeetCode Binary Tree Maximum Path Sum 二叉树最大路径和(DFS)的更多相关文章

  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(最大路径和)

    Given a binary tree, find the maximum path sum. The path may start and end at any node in the tree. ...

  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

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

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

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

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

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

  9. [leetcode]Binary Tree Maximum Path Sum @ Python

    原题地址:https://oj.leetcode.com/problems/binary-tree-maximum-path-sum/ 题意: Given a binary tree, find th ...

随机推荐

  1. 如何提高SQL的执行效率

    一.因情制宜,建立“适当”的索引 建立“适当”的索引是实现查询优化的首要前提. 索引(index)是除表之外另一重要的.用户定义的存储在物理介质上的数据结构.当根据索引码的值搜索数据时,索引提供了对数 ...

  2. Linux - wxWidgets安装和编译HelloWorld

    安装参考http://codelite.org/LiteEditor/WxWidgets30Binaries#toc2 源 /etc/apt/source.list deb http://repos. ...

  3. const以及入栈出栈

    #include "stdafx.h"#include <iostream>using namespace std; class StringStack{ enum{s ...

  4. hdu 4472 Count

    递推,一般的dp值: #include<stdio.h> #include<string.h> #define mod 1000000007 ]; int Dp() { a[] ...

  5. LoadRunner8 安装步骤

    一.介绍 LoadRunner,是一种预测系统行为和性能的负载测试工具.通过以模拟上千万用户实施并发负载及实时性能监测的方式来确认和查找问题,LoadRunner能够对整个企业架构进行测试.企业使用L ...

  6. linux命令(4):ps命令

    Linux中的ps命令是Process Status的缩写.ps命令用来列出系统中当前运行的那些进程.ps命令列出的是当前那些进程的快照,就是执行ps命令的那个时刻的那些进程,如果想要动态的显示进程信 ...

  7. 【推荐】《Netty in action》书籍

    最近准备开始阅读一下<Netty in action>并且准备构架设计一个分布式系统.用于新项目. 貌似压力很大啊.压力就是东西.希望自己能够调节好. Netty in action是Ne ...

  8. jdbc框架 commons-dbutils的使用

    commons-dbutils 是 Apache 组织提供的一个开源 JDBC工具类库,它是对JDBC的简单封装,学习成本极低,并且使用dbutils能极大简化jdbc编码的工作量,同时也不会影响程序 ...

  9. 索引 split2

    当往一个已经满了的索引块中插入新的索引条目时,将发生索引块的split,在9i下,分两种不同的情况进行split: (1)如果插入的索引键值不是最大的,将发生50-50的split,也就是说有bloc ...

  10. env1

    给新手参考的Cadence Allegro快捷键 原文链接:http://www.eda365.com/thread-30935-1-1.html 发上我的快捷键给新手参考,我也是在LULU给我的EN ...