Lint Code 二叉树中的最大路径和
来源:互联网 发布:linux查看硬盘情况 编辑:程序博客网 时间:2024/05/18 22:50
题目描述:
给出一棵二叉树,寻找一条路径使其路径和最大,路径可以在任一节点中开始和结束(路径和为两个节点之间所在路径上的节点权值之和)
您在真实的面试中是否遇到过这个题? Yes
样例
给出一棵二叉树:
1 / \ 2 3
返回 6
ac代码:
/** * Definition of TreeNode: * class TreeNode { * public: * int val; * TreeNode *left, *right; * TreeNode(int val) { * this->val = val; * this->left = this->right = NULL; * } * } */class Solution {public: /** * @param root: The root of binary tree. * @return: An integer */ int dfs(TreeNode *root,int &maxx) { if(root==NULL) return 0; int r,l; r=dfs(root->right,maxx); l=dfs(root->left,maxx); //cout<<r<<" "<<l<<endl; maxx=max(maxx,max(r,0)+max(l,0)+root->val); return max(0,max(l,r))+root->val; } int maxPathSum(TreeNode *root) { // write your code here int maxx=-0x3f3f3f3f; dfs(root,maxx); return maxx; }};
阅读全文
0 0
- Lint Code 二叉树中的最大路径和
- 二叉树中路径和为某整数的所有路径 lint code by python
- LintCode-二叉树中的最大路径和
- 二叉树中的最大路径和
- 二叉树中的最大路径和
- 题目:二叉树中的最大路径和
- 二叉树中的最大路径和
- 二叉树中的最大路径和
- lintcode,二叉树中的最大路径和
- 二叉树中的最大路径和
- 二叉树中的最大路径和
- 二叉树中的最大路径和-LintCode
- LintCode 二叉树中的最大路径和
- lintcode--二叉树中的最大路径和
- 二叉树中的最大路径和-LintCode
- 二叉树中的最大路径和
- 二叉树最大路径和
- 二叉树最大路径和
- android eclipse 项目 javaapk 精选
- React
- C++ const 限定符
- spring的静态变量注入值
- C++ Primer 关联容器 单词转换程序
- Lint Code 二叉树中的最大路径和
- 「python」函数使用注意事项
- java 图形用户界面 swing 之 复选框 及示例程序
- 关于AngularJS的系列
- MySQL的btree索引和hash索引的区别
- 动态规划专项训练(2)
- GSensorView自动感应的View
- 深度学习笔记(九)AutoEncoder自动编码器
- Canvas和Bitmap的关系