首页 > 其他 > 详细

LeetCode OJ Binary Tree Maximum Path Sum

时间:2015-03-31 17:59:53      阅读:226      评论:0      收藏:0      [点我收藏+]

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 max(int a, int b) {
	if (a > b) return a;
	return b;
}

int ans;

int maxDownSum(struct TreeNode * now) {
	if (!now) return 0;
	int l = max(0, maxDownSum(now->left));
	int r = max(0, maxDownSum(now->right));
	ans = max(ans, l + r + now->val);
	return max(l, r) + now->val;
}

int maxPathSum(struct TreeNode * root) {
	ans = INT_MIN;
	maxDownSum(root);
	return ans;
}

LeetCode OJ Binary Tree Maximum Path Sum

原文:http://blog.csdn.net/u012925008/article/details/44782685

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!