[LeetCode] Binary Tree Maximum Path Sum
来源:互联网 发布:linux培训上海 编辑:程序博客网 时间:2024/06/05 20:43
Given a binary tree, find the maximum path sum.
The path may start and end at any node in the tree.
For example:
Given the below binary tree,
1 / \ 2 3
Return 6
.
/** * 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 maxx(TreeNode *root){ if(root == NULL) return 0; int s1 = maxx(root -> left); int s2 = maxx(root -> right); int num = root -> val; if(s1 > 0) num += s1; if(s2 > 0) num += s2; if(ans < num) ans = num; return max(root -> val,max(s1,s2) + root -> val); } int maxPathSum(TreeNode *root) { ans = root -> val; maxx(root); return ans; }};
0 0
- *****(leetcode) Binary Tree Maximum Path Sum (tree)
- LeetCode Binary Tree Maximum Path Sum
- [Leetcode] Binary Tree Maximum Path Sum
- [leetcode] Binary Tree Maximum Path Sum
- LeetCode Binary Tree Maximum Path Sum
- [LeetCode] Binary Tree Maximum Path Sum
- leetcode Binary Tree Maximum Path Sum
- 【leetcode】Binary Tree Maximum Path Sum
- LeetCode - Binary Tree Maximum Path Sum
- [LeetCode]Binary Tree Maximum Path Sum
- [Leetcode]Binary Tree Maximum Path Sum
- Leetcode: Binary Tree Maximum Path Sum
- LeetCode-Binary Tree Maximum Path Sum
- [leetcode] Binary Tree Maximum Path Sum
- LeetCode - Binary Tree Maximum Path Sum
- LeetCode 124: Binary Tree Maximum Path Sum
- 【Leetcode】Binary Tree Maximum Path Sum
- LeetCode | Binary Tree Maximum Path Sum
- 字符串加解密
- Perl 去掉行分割符
- 【Objective-C】06-点语法
- [水题][第一阶段-英文题试水][HDOJ-2734]Quicksum
- Android生成二维码图片
- [LeetCode] Binary Tree Maximum Path Sum
- iOS 8 AutoLayout与Size Class自悟
- duplicate symbols for architecture armv7错误
- Android ANT 多渠道打包解决方案
- 2014年总结
- android中的 AlertDialog的个人见解
- 【session内部机制】PHP的session_set_save_handler 执行机制
- Python unittest parametrized test cases
- linux工具安装