码迷,mamicode.com
首页 > 其他好文 > 详细

LeetCode OJ Binary Tree Maximum Path Sum

时间:2015-03-31 18:01:18      阅读:119      评论:0      收藏:0      [点我收藏+]

标签:leetcode

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

标签:leetcode

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!