第一选择是将其转化成图用动态规划,但这样还是太麻烦

使用递归的思路,对于当前的节点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二叉树最大路径和的更多相关文章

  1. [LeetCode] Path Sum III 二叉树的路径和之三

    You are given a binary tree in which each node contains an integer value. Find the number of paths t ...

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

  3. LintCode-376.二叉树的路径和

    二叉树的路径和 给定一个二叉树,找出所有路径中各节点相加总和等于给定 目标值 的路径. 一个有效的路径,指的是从根节点到叶节点的路径. 样例 给定一个二叉树,和 目标值 = 5: 返回: [      ...

  4. Java实现求二叉树的路径和

    题: 解: 这道题考的是如何找出一个二叉树里所有的序列. 我的思路是先从根节点开始遍历,找出所有的子节点,因为每个子节点只有一个父节点,再根据每个子节点向上遍历找出所有的序列,再判断序列的总和. 这样 ...

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

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

  7. 【1】[leetcode-124] 二叉树中的最大路径和

    (没做出来,典型题目重要) 二叉树中的最大路径和(hard) 给定一个非空二叉树,返回其最大路径和. 本题中,路径被定义为一条从树中任意节点出发,达到任意节点的序列.该路径至少包含一个节点,且不一定经 ...

  8. LeetCode-124.二叉树中的最大路径和

    给定一个非空二叉树,返回其最大路径和. 本题中,路径被定义为一条从树中任意节点出发,达到任意节点的序列.该路径至少包含一个节点,且不一定经过根节点. 示例 1: 输入: [1,2,3] 1 / \ 2 ...

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

随机推荐

  1. qt tableview中如何添加右键菜单且不可编辑单元格

    QTableView是一个比较实用的类,下面教给大家如何在QTableView中添加右键菜单. #include <QMenu>#include <QAction> QTabl ...

  2. 使用eclipse根据wsdl生成客户端

    1.在需要生成的java项目右键new →other→ Web Service Client . 2.点击 Next.进入下面的界面,选择Brown...,选择WSDL,然后选择Next 3.Serv ...

  3. PXC集群的概述及搭建

    目录 PXC集群的概述及搭建 PXC集群的简介 PXC集群主要由两部分组成: PXC的特性和优点: PXC的局限和劣势: PXC原理描述 在Centos部署基于Mysql高可用方案操作过程 新增节点加 ...

  4. Zabbix 监控Windows磁盘IO

    Windows下,打开cmd输入 typeperf -qx > c:\typeperf.txt #打开c:\typeperf.txt文件 windows性能计数器里面包含windows相关数值 ...

  5. 网络协议相关面试问题-DNS相关面试问题

    对于网络上的大部通讯都是基于TCP/IP协议的, 其中最重要的是IP协议,它是基于IP地址的,而计算机通讯只能识别IP地址,如192.168.0.1,而不能识别像咱们在浏览器敲得见名之义的" ...

  6. 【2017中国大学生程序设计竞赛-哈尔滨站】B - K-th Number

    原题: 题意: 给你一个长度为N的正整数组A,对于这个数组的所有子区间,若长度小于k则不管它,若长度大于等于k则取第k大放入数组B 问你B中第M大的数是谁 一眼序列分治,然而没思路 数据结构?能想到从 ...

  7. 测试工具jmeter

    测试工具jmeter http:压力测试 https://www.cnblogs.com/stulzq/p/8971531.html

  8. Solr——Java应用

    Solr有一个客户端SolrJ 创建一个Java Project 引入Jar包 添加test类 package com.solr.test; import java.io.IOException; i ...

  9. LeetCode 01 两数之和

    链接:https://leetcode-cn.com/problems/two-sum 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们 ...

  10. input和textarea的区别

    区别: <textarea>标签是成对的,有结束标签进行闭合,标签的内容写在标签对中间:<input>是单个标签,标签的内容通过 value 属性设置: <textare ...