Binary Tree Maximum Path Sum

原创 2015年11月20日 16:31:17
  1. 问题

    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,

           1
          / \
         2   3
    

    Return 6.

  2. 解答
    class Solution1 {
    public:
        int maxPathSum(TreeNode* root)
        {
            if(!root) return 0;
            if(!root->left && !root->right) return root->val;
            else
            {
                int s1=maxPathSum(root->left);
                int s2=maxPathSum(root->right);
                
                max=0;
                path(root->left,0);
                int s3=max;
                
                max=0;
                path(root->right,0);
                int s4=max;
                
                if(s3+s4+root->val>s1 && s3+s4+root->val>s2)
                    return s3+s4+root->val;
                else
                {
                    return (s1>s2)?s1:s2;
                }
            }
        }
        
    public:
        void path(TreeNode *root,int sum)
        {
             if(root)
            {
              if(root->left==NULL && root->right==NULL)
              {
                  //a.push_back(root->val);
                  sum+=root->val;
                  if(sum>max)
                    max=sum;
              }
             else
              {
                sum+=root->val;
                if(sum>max)
                    max=sum;
                path(root->left,sum);
                path(root->right,sum);
              }
            }
        }
    public:
       int max;
    };
    
    /**
     * Definition for binary tree
     * struct TreeNode {
     *     int val;
     *     TreeNode *left;
     *     TreeNode *right;
     *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
     * };
     */
    class Solution {
    public:
        int ans;
        int scanT(TreeNode* root)
        {
            if(root == NULL) return 0;
            
            int left = scanT(root -> left);
            int right = scanT(root -> right);
            
            int val = root -> val;
            if(left > 0) val += left;
            if(right > 0) val += right;
            
            if(val > ans) ans = val;
            
            return max(root->val ,max(left +  root -> val , right + root -> val));
        }
        int maxPathSum(TreeNode *root) {
            // Start typing your C/C++ solution below
            // DO NOT write int main() function
            if(root == NULL) return 0;
            ans = root -> val;
            scanT(root);
            return ans;
        }
    };


版权声明:本文为博主原创文章,未经博主允许不得转载。

LeetCode: Binary Tree Maximum Path Sum [124]

【题目】 Given a binary tree, find the maximum path sum. The path may start and end at any node in the...

#94 Binary Tree Maximum Path Sum

题目描述: Given a binary tree, find the maximum path sum. The path may start and end at any node...

LeetCode(124) Binary Tree Maximum Path Sum

题目 Given a binary tree, find the maximum path sum. For this problem, a path is defined as any seq...
  • fly_yr
  • fly_yr
  • 2015年12月27日 16:20
  • 1768

LeetCode | Binary Tree Maximum Path Sum(二叉树的路径最大值)

Given a binary tree, find the maximum path sum. The path may start and end at any node in the tre...

124. Binary Tree Maximum Path Sum

Given a binary tree, find the maximum path sum. For this problem, a path is defined as any sequen...

Leetcode 124. Binary Tree Maximum Path Sum 最大路径和 解题报告

1 解题思想在二叉树当中,找到一条路径,这条路径的和最大。这里所谓的路径,就是每个节点只经过一次的一条路径,可以选择在任何节点开始,也可以选择在任何节点结束本质上,其有点像用DP的递归方式搜索,因为每...
  • MebiuW
  • MebiuW
  • 2016年10月13日 19:13
  • 476

LeetCode 124. Binary Tree Maximum Path Sum

1. 题目描述 Given a binary tree, find the maximum path sum. For this problem, a path is defined a...

[LeetCode 124] Binary Tree Maximum Path Sum (DFS/二叉树)

递归求解二叉树
  • zlasd
  • zlasd
  • 2016年09月21日 12:03
  • 203

124:Binary Tree Maximum Path Sum【树】【DFS】

题目链接:click~ /*题意:给定一颗二叉树,求最大路径和。路径可以从任意结点开始,任意结点结束*/ /** *思路: 对于每个结点,需要比较三个值 * 1)左子结点为结尾的路...

LEETCODE 124. Binary Tree Maximum Path Sum

题目大意给出一棵二叉树,求出里面的每个节点加起来值最大的路径的和。解题思路解决这一题的思路主要是遍历每一个点i,计算以这个点为根节点,并经过这个点的路径和的最大值,记作pathSum(i),这个值可...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Binary Tree Maximum Path Sum
举报原因:
原因补充:

(最多只允许输入30个字)