112_leetcode_Binary Tree Maximum Path
来源:互联网 发布:主宰西游装备进阶数据 编辑:程序博客网 时间:2024/06/09 17:33
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
.
int maxPathSum(TreeNode *root) { if(root == NULL) { return 0; } if(!root->left && !root->right) { return root->val; } int value = INT_MIN; maxPathSumCore(root, value); return value; } int maxPathSumCore(TreeNode *root, int &value) { if(root == NULL) { return 0; } int leftLength = maxPathSumCore(root->left, value); int rightLength = maxPathSumCore(root->right, value); int curLength = max(root->val, root->val + max(leftLength, rightLength)); int tempValue = max(curLength, leftLength + rightLength + root->val); if(tempValue > value) { value = tempValue; } return curLength; }
0 0
- 112_leetcode_Binary Tree Maximum Path
- 66_leetcode_Binary Tree Inorder Traversal
- 78_leetcode_Binary Tree Postorder Traversal
- 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
- Java反射机制
- 'IDispatchEx' : redefinition 对应duilib
- Bitmap too large to be uploaded into a texture,Hardware accelerated
- 邮件营销避免进入垃圾箱技巧分享
- 基于CodeMirror&TernJS的汉语Javascript编辑器(和翻译器)
- 112_leetcode_Binary Tree Maximum Path
- 实现基于DNS的负载均衡
- 000:以NotePad++作为Python IDE
- poj 3204 Ikki's Story I - Road Reconstruction && zoj 2532 Internship 网络流关键边
- [LeetCode] Valid Number
- Apache AB 第一个测试案例及测试结果解释
- 黑马程序员-C语言编程中include的用法
- 程序员必须知道的10大基础实用算法及其讲解
- 两个输出文件名解析为同一个输出路径:“obj\x86\Debug\SafetyValve.FormNewTest.resources”SafetyValve