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 of nodes from some starting node to any node in the tree along the parent-child connections. The path does not need to go through the root.
For example:
Given the below binary tree,
- 1
- / \
- 2 3
Return 6
.
=============
题目:树中联通路径中,路径元素和最大值?
路径利用父子关系相连,不必经过根节点root
路径可以从任意节点开始,到任意节点.
=====
思路:
这道题的解法,来自网络leetcode150题集合,
利用dfs遍历树的方式,传递每个树父子节点间的路径大小;
利用一个全局遍历来时记住max_sum来记录已经找到的最大的路径值,
最难理解的是怎么在 dfs遍历树的过程,传递路径信息?
-----
借用"最大连续子序列和"问题的思路,array只有一个方向,
但是Tree有左右两个方向,我们需要比较两个方向上的值.
先算出左右子树的结果L和R.
如果L>0,那么对后序结果是有利的,后序中加上L,(不是向max_sum中,而是向如节点中)
如果R>0,那么对后序结果是有利的,加上R
---
==========
代码如下:
- /**
- * 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 help_maxPathSum(int &max_sum,TreeNode *root){
- if(root==nullptr) return ;
- int l = help_maxPathSum(max_sum,root->left);
- int r = help_maxPathSum(max_sum,root->right);
- int sum = root->val;
- if(l>) sum += l;
- if(r>) sum += r;
- max_sum = max(max_sum,sum);
- return max(r,l)>? max(r,l)+root->val:root->val;//只能向上传递 左子树或者 右子树的有利值
- }
- int maxPathSum(TreeNode* root) {
- int max_sum = INT_MIN;
- help_maxPathSum(max_sum,root);
- return max_sum;
- }
- };
124. Binary Tree Maximum Path Sum的更多相关文章
- 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 ...
- 第四周 Leetcode 124. Binary Tree Maximum Path Sum (HARD)
124. Binary Tree Maximum Path Sum 题意:给定一个二叉树,每个节点有一个权值,寻找任意一个路径,使得权值和最大,只需返回权值和. 思路:对于每一个节点 首先考虑以这个节 ...
- 【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 ...
- [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 ...
- 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 ...
- 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 ...
- 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 ...
- [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 ...
- 124. Binary Tree Maximum Path Sum *HARD* -- 二叉树中节点和最大的路径的节点和
Given a binary tree, find the maximum path sum. For this problem, a path is defined as any sequence ...
随机推荐
- android 点击edittext弹出软键盘,否则不弹
只需要加android:windowSoftInputMode="stateHidden|stateAlwaysHidden"就可以 如:<activity android: ...
- ZOJ 1041 Transmitters
原题链接 题目大意:有一个发射站,覆盖范围是半径一定的一个半圆.在一个1000*1000平方米的地盘里有很多接收站.给定发射站的圆心,求最佳角度时能覆盖接收站的个数. 解法:本质上就是给一个原点和其他 ...
- c#部分---用结构体的题目- //请输入班级人数,输入每个人的学号,姓名,和语文分数、数学分数和英语分数(要求使用结构体)
//请输入班级人数,输入每个人的学号,姓名,和语文分数.数学分数和英语分数(要求使用结构体), //求班级里两个语文分数是最高分的学生的所有信息:数学分数是最高分的两个学生的所有信息:英语平均分 建立 ...
- C#部分---语言经典题目——兔子生兔子
根据本月成兔=上月成兔+上月小兔:本月小兔=上月幼兔:本月幼兔=本月成兔 利用while循环: Console.WriteLine("请输入月份:"); //int m = int ...
- 越狱Season 1-Episode 12:Odd Man Out
Season 1-Episode 12:Odd Man Out -Sorry to keep you waiting. 抱歉让你等了半天 -Oh, it's, uh, not a problem. 嗯 ...
- 谷歌浏览器chrome与firefox的冲突(未解之谜)
那年,公司开发了一套在线制作电子书的系统 e-textbook. 我负责小学电脑科教材在线题目的制作. 利用 ps制作剪裁好图片,导入系统,制作题目,并通知同事添加代码. 检测时,却发现有一道图片拖放 ...
- dir:一行代码,提取出所有视频文件名称及路径
某次,部门接到一个任务,要求对公司现有的视频文件资料做一个统计整理分类的工作. 领导召集开会,问:两周时间够用吗? 统计整理分类工作的第一步骤是把视频文件名称来源类别信息录入到 excel 表格中,才 ...
- H2 database 行相加-行列转换
create or replace view view_acceptCompanyasselect * from (select WARNIGID,max(CASEWHEN(zhtablename ...
- div+css关于overflow 动态滚动效果
http://www.ablanxue.com/prone_2613_1.html 关于overflow:hidden不起作用的说明
- ABBYY PDF Transformer+从文件选项中创建PDF文档的教程
可使用OCR文字识别软件ABBYY PDF Transformer+从Microsoft Word.Microsoft Excel.Microsoft PowerPoint.HTML.RTF.Micr ...