偶然在面试题里面看到这个题所以就在Leetcode上找了一下,不过Leetcode上的比较简单一点。

题目:

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

For example:
Given the below binary tree,

       1
/ \
2 3

Return 6.

暴力解法就是将每一个节点作为开始节点,寻找最大的路径长度,感觉和在一个整数序列中寻找和最大的子序列差不多,不同的是每一个开始节点有几条不同的路。再仔细想一想就知道,每一条路径肯定都有一个最高的节点,把每一个节点作为一条路径的最高节点并且是这个路径的末端节点,可以应用递归来解决这个问题。假设节点x为这条路径的最高节点和末端节点,求得这条路径的函数为maxpath(x)=max(maxpath(x->left)+x->val, maxpath(x->right)+x->val, x->val) 。调用根节点的maxpath可以计算出以每个节点为最高节点和末端节点的路径的最大值,那以该节点为最高节点的路径的最大值为maxpath(x)或者maxpath(x->left)+maxpath(x->right)+x->val,可以用MAX记录这个值,最后函数返回MAX

/**
* 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 MAX = -;
int maxPathSum(TreeNode* root) {
maxpath(root);
return MAX;
}
int maxpath(TreeNode *node){
if(node == NULL)
return ;
else{
int x = maxpath(node->left), y = maxpath(node->right);
int maxlen = x+node->val > node->val? x+node->val:node->val;
int result = maxlen > y+node->val?maxlen:y+node->val;
MAX = result>MAX?result:MAX;
MAX = x+y+node->val>MAX? x+y+node->val:MAX;
return result;
}
} };

C++ leetcode Binary Tree Maximum Path Sum的更多相关文章

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

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

  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 求二叉树的最大路径和

    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

    1.题目说明 Given a binary tree, find the maximum path sum.   The path may start and end at any node in t ...

  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]Binary Tree Maximum Path Sum @ Python

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

  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 二叉树最大路径和(DFS)

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

随机推荐

  1. 变量存储缓存机制 Number (int bool float complex)

    # ###变量存储的缓存机制(为了节省空间) #Number (int bool float complex) # (1) int -5~正无穷范围内 var1 = 18 var2 = 18 var1 ...

  2. openshift 容器云从入门到崩溃之五《部署应用》

    1.配置部署模板 配置好用户权限之后就可以部署应用了oc常用的两种部署方式: Deploy Image方式 优点:这种方式是最简单的部署方式,你只需要有一个容器镜像就行了或者公开的docker hub ...

  3. pytorch torchvision对图像进行变换

    class torchvision.transforms.Compose(转换) 多个将transform组合起来使用. class torchvision.transforms.CenterCrop ...

  4. Lucene的深入

    Lucene创建索引 第一.创建文档对象 第二.创建Field对象(各种类型)整数String.Text.Long.Float.Double等... Field对象 包含三大属性:是否分词,是否索引, ...

  5. shell爬虫--抓取某在线文档所有页面

    在线教程一般像流水线一样,页面有上一页下一页的按钮,因此,可以利用shell写一个爬虫读取下一页链接地址,配合wget将教程所有内容抓取. 以postgresql中文网为例.下面是实例代码 #!/bi ...

  6. pageUtil分页工具

    分页工具: https://www.cnblogs.com/ggq-insist-qiang/articles/10095603.html

  7. HTML5的自定义属性的使用总结

    https://blog.csdn.net/qq_31851435/article/details/53100691 <div id="myDiv" data-attribu ...

  8. java基础hashmap

    Iterator中hasNext(), next() 在Iterator类中,我们经常用到两个方法: hasNext(), next(),具体含义: next(),  是返回当前元素, 并指向下一个元 ...

  9. PotPlayer安装与配置

    目录 1.简介 2.安装 3.设置 基本选项设置: 播放选项设置: PotPlayer皮肤设置: 1.简介 PotPlayer一款小巧简单的视频播放软件,具有于强大的定制能力和个性化功能. 2.安装 ...

  10. 移动Web UI库(H5框架)有哪些,看这里就够了

    前言 今年上半年,项目组在项目开发的过程中建立了一套风格统一,组件丰富完善,命名统一规范的PC端UI库,适用于做大型站点,该UI库也是应用到了整个平台的项目中,在各个项目组中进行推广.因为项目的保密性 ...