Binary Tree Maximum Path Sum
来源:互联网 发布:linux编译安装php5.6 编辑:程序博客网 时间:2024/05/02 23:44
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.
求树的一点到另一点的最大路径,利用递归的方法,ans 在 左子树,右子树,root+左+右的最大中产生。
/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Solution {public: int ans; int maxPathSum(TreeNode *root) { ans = INT_MIN; travel(root); return ans; } int travel(TreeNode * root){ if(root == NULL) return 0; int sum = root->val; int leftsum = travel(root->left); int rightsum = travel(root->right); if(leftsum > 0) sum += leftsum; if(rightsum > 0) sum += rightsum; ans = max(ans,sum); sum = max(leftsum,rightsum); return root->val + (sum > 0 ? sum : 0); }};
0 0
- Binary Tree Maximum Path Sum
- Binary Tree Maximum Path Sum
- Binary Tree Maximum Path Sum
- Binary Tree Maximum Path Sum
- Binary Tree Maximum Path Sum
- Binary Tree Maximum Path Sum
- Binary Tree Maximum Path Sum
- Binary Tree Maximum Path Sum
- Binary Tree Maximum Path Sum
- Binary Tree Maximum Path Sum
- Binary Tree Maximum Path Sum
- Binary Tree Maximum Path Sum
- Binary Tree Maximum Path Sum
- Binary Tree Maximum Path Sum
- Binary Tree Maximum Path Sum
- Binary Tree Maximum Path Sum
- Binary Tree Maximum Path Sum
- Binary Tree Maximum Path Sum
- SpriteKit框架(动态文字)
- shell 中数学计算总结
- 相互攀比导致如今家乡社会风气“变质”!真是金钱万能啊!!
- 连接数据的网络交互
- 建立yum 仓库
- Binary Tree Maximum Path Sum
- [知其然不知其所以然-18] handle_mm_fault, bad page等错误现场分析
- Swift UIAlertView(警告视图)、UIActionSheet(操作表)
- invalid context 0x0
- 工作需要用到C++中的正则表达式,所以就研究了以上三种正则。 http://www.cnblogs.com/pmars/archive/2012/10/24/2736831.html
- 在centos 6.6 上搭建一个简单的php的开发环境
- springMVC- The prefix "mvc" for element "mvc:annotation-driven" is not bound 解决方法
- 基于ubuntu下stm32开发环境搭建(ubuntu+st-link+libopencm3)
- Shader的语法