*leetcode #124 in cpp
2016-06-20 04:45
363 查看
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,
Return
Solution:
This question is pretty hard. I did not figure it out until I searched for solution online.
Code:
/**
* 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 maxPathSum(TreeNode* root) {
int maxx = INT_MIN;
findMaxPathSum(root, maxx);
return maxx;
}
int findMaxPathSum(TreeNode *node,int &maxx){ //this function returns the max path sum from node to its successors i.
if(!node) return 0;
int left = findMaxPathSum(node->left,maxx);
int right = findMaxPathSum(node->right,maxx);
int temp = max(max(node->val, node->val + left),node->val + right);
maxx = max(max(temp,node->val+left + right), maxx);//record the max sum at node, by comparing max path sum that starts at node, and max path sum that goes from left child to node to right child.
return temp;
}
};
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.
Solution:
This question is pretty hard. I did not figure it out until I searched for solution online.
Code:
/**
* 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 maxPathSum(TreeNode* root) {
int maxx = INT_MIN;
findMaxPathSum(root, maxx);
return maxx;
}
int findMaxPathSum(TreeNode *node,int &maxx){ //this function returns the max path sum from node to its successors i.
if(!node) return 0;
int left = findMaxPathSum(node->left,maxx);
int right = findMaxPathSum(node->right,maxx);
int temp = max(max(node->val, node->val + left),node->val + right);
maxx = max(max(temp,node->val+left + right), maxx);//record the max sum at node, by comparing max path sum that starts at node, and max path sum that goes from left child to node to right child.
return temp;
}
};
相关文章推荐
- leetcode 179 Largest Number
- leetcode 24 Swap Nodes in Pairs
- leetcode 2 Add Two Numbers 方法1
- leetcode 2 Add Two Numbers 方法2
- C++中const用法总结
- CPP 虚函数、虚函数表及虚拟继承(转)
- leetcode----Longest Substring Without Repeating Characters
- [LeetCode]47 Permutations II
- [LeetCode]65 Valid Number
- [LeetCode]123 Best Time to Buy and Sell Stock III
- [LeetCode] String Reorder Distance Apart
- [LeetCode] Sliding Window Maximum
- [LeetCode] Find the k-th Smallest Element in the Union of Two Sorted Arrays
- [LeetCode] Determine If Two Rectangles Overlap
- [LeetCode] A Distance Maximizing Problem
- leetcode_linearList
- leetcode_linearList02
- Deploying Control Plane Policing
- 021-Merge Two Sorted Lists(合并两个排好序的单链表);leetcode
- LeetCode[Day 1] Two Sum 题解