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

Problem Binary Tree Maximum Path Sum

时间:2014-07-07 15:57:05      阅读:252      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   color   strong   art   

Problem Description:

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.

Solution:

 1 public class Solution {
 2     public int max;
 3     public int maxPathSum(TreeNode root) {
 4         if (root == null) return 0;
 5         max = root.val;
 6         findMax(root);
 7         return max;
 8     }
 9     public  int findMax(TreeNode node) {
10         if (node == null) return 0;
11 
12         int left = Math.max(findMax(node.left), 0);
13         int right = Math.max(findMax(node.right), 0);
14 
15         max = Math.max(node.val + left + right, max);
16 
17         return node.val + Math.max(left, right);
18     }
19 }

 

Problem Binary Tree Maximum Path Sum,布布扣,bubuko.com

Problem Binary Tree Maximum Path Sum

标签:des   style   blog   color   strong   art   

原文地址:http://www.cnblogs.com/liew/p/3815118.html

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