leetcode124二叉树最大路径和

第一选择是将其转化成图用动态规划,但这样还是太麻烦
使用递归的思路,对于当前的节点root,分别求左右孩子到当前节点的单项路径权值最大的路径权值,然后记包含当前节点的路径权值为 path_price=root->val+left_gain+right_gain,取sum_max和他较大的;
返回左右孩子权值最大的单向路径(只往上不拐弯)的权值(这需要好好理解,因为更新权值的式子是root->val+left_gain+right_gain,即从左子路到根再到右子路的一条式子,所以计算left_gain与right_gain需要返回这样的值)
/**
* 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) {
//化为带权的图,求最大路径
//递归,分解问题
max_gain(root);
return sum_max;
}
int max_gain(TreeNode* root){
if(root==NULL) return ; //0代表不选择该路径
int left_gain=max(max_gain(root->left),);
int right_gain=max(max_gain(root->right),); int path_price=root->val+left_gain+right_gain;
sum_max=max(path_price,sum_max); //返回值//左右选一条是因为递归时,需要从子节点选一条路径到当前根节点;而当前最大值由sum_max记录
return root->val+max(left_gain,right_gain);
}
private:
int sum_max=INT_MIN;
};
leetcode124二叉树最大路径和的更多相关文章
- [LeetCode] Path Sum III 二叉树的路径和之三
You are given a binary tree in which each node contains an integer value. Find the number of paths t ...
- [LeetCode] Path Sum IV 二叉树的路径和之四
If the depth of a tree is smaller than 5, then this tree can be represented by a list of three-digit ...
- LintCode-376.二叉树的路径和
二叉树的路径和 给定一个二叉树,找出所有路径中各节点相加总和等于给定 目标值 的路径. 一个有效的路径,指的是从根节点到叶节点的路径. 样例 给定一个二叉树,和 目标值 = 5: 返回: [ ...
- Java实现求二叉树的路径和
题: 解: 这道题考的是如何找出一个二叉树里所有的序列. 我的思路是先从根节点开始遍历,找出所有的子节点,因为每个子节点只有一个父节点,再根据每个子节点向上遍历找出所有的序列,再判断序列的总和. 这样 ...
- [LeetCode] 666. Path Sum IV 二叉树的路径和 IV
If the depth of a tree is smaller than 5, then this tree can be represented by a list of three-digit ...
- [Swift]LeetCode124. 二叉树中的最大路径和 | 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 ...
- 【1】[leetcode-124] 二叉树中的最大路径和
(没做出来,典型题目重要) 二叉树中的最大路径和(hard) 给定一个非空二叉树,返回其最大路径和. 本题中,路径被定义为一条从树中任意节点出发,达到任意节点的序列.该路径至少包含一个节点,且不一定经 ...
- LeetCode-124.二叉树中的最大路径和
给定一个非空二叉树,返回其最大路径和. 本题中,路径被定义为一条从树中任意节点出发,达到任意节点的序列.该路径至少包含一个节点,且不一定经过根节点. 示例 1: 输入: [1,2,3] 1 / \ 2 ...
- [LeetCode] Path Sum 二叉树的路径和
Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all ...
随机推荐
- python制作一个简单词云
首先需要安装三个包:# 安装:pip install matplotlib# 安装:pip install jieba# 安装pip install wordcloud 1.制作英文字母的词云 效果图 ...
- Invalid property value
又见这个错误!头几天同事遇到这个问题,我查到去年写的并按此解决了,原文在这里,查了半天,才查出是ftShortInt造成的这个错误. 当我们在设计期将ClientQuery.Active设置为True ...
- Windows下Mysql 用户忘记密码时修改密码
一般这种情况都可以用安全模式下修改来解决.安全模式下即跳过权限检查,输入账号后直接登录进mysql 1.使用管理员权限打开dos窗口,进入mysql安装目录的bin文件夹下,将Mysql服务关闭 sc ...
- 树莓派3B+一个外接显示器影响有线网卡无法启动的BUG
在给一块用了不到一年的树莓派3B+重装官方系统时发现了一件诡异的事情,树莓派的有线网络会在重启后自动停掉,只有无线模块正常.即右上角的网络图标显示一个红叉叉. 在多次重装未果后,发现一个命令可以让有线 ...
- IDEA利用mybatis-generator自动生成dao和mapper
pom.xml配置 <properties> <java.version>1.8</java.version> <mybatis-generator-core ...
- vue+django前后端项目部署
一.python3的安装 1.安装python前的库环境: yum install gcc patch libffi-devel python-devel zlib-devel bzip2-devel ...
- ajax 向php发送请求
<html> <head> <script src="clienthint.js"></script> </head> ...
- 查看 SharePoint Server 中的所有网站集
网站集是具有同一所有者并共享管理设置(例如权限和配额)的一组网站.网站集是在 Web 应用程序中创建的.创建网站集时,将自动在网站集中创建一个首要网站.然后,可以在首要网站下创建一个或多个子网站.首要 ...
- 自动生成SSM框架
使用idea 新创建项目 然后 新创建 java .resources 文件夹...... 图上是项目结构 java文件夹下的 文件夹 命名规范 com.nf147(组织名)+ oukele(作者) ...
- js 原生ajax请求
什么是ajax 所有现代浏览器均支持 XMLHttpRequest 对象(IE5 和 IE6 使用 ActiveXObject). XMLHttpRequest 用于在后台与服务器交换数据.这意味着可 ...