zoukankan      html  css  js  c++  java
  • 124. Binary Tree Maximum Path Sum

    Given a non-empty 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 must contain at least one node and does not need to go through the root.

    Example 1:

    Input: [1,2,3]
    
           1
          / 
         2   3
    
    Output: 6
    

    Example 2:

    Input: [-10,9,20,null,null,15,7]
    
       -10
       / 
      9  20
        /  
       15   7
    
    Output: 42

    Approach #1: C++.

    /**
     * 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) {
            if (!root) return 0;
            int ans = INT_MIN;
            solve(root, ans);
            return ans;
        }
    private:
        int solve(TreeNode* root, int& ans) {
            if (!root) return 0;
            int l = max(0, solve(root->left, ans));
            int r = max(0, solve(root->right, ans));
            int sum = l + r + root->val;
            ans = max(ans, sum);
            return max(l, r) + root->val;
        }
    };
    

      

    Approach #2: Java.

    /**
     * Definition for a binary tree node.
     * public class TreeNode {
     *     int val;
     *     TreeNode left;
     *     TreeNode right;
     *     TreeNode(int x) { val = x; }
     * }
     */
    class Solution {
        int ans = Integer.MIN_VALUE;
        public int maxPathSum(TreeNode root) {
            if (root == null) return 0;
            solve(root);
            return ans;
        }
        
        private int solve(TreeNode root) {
            if (root == null) return 0;
            int l = Math.max(0, solve(root.left));
            int r = Math.max(0, solve(root.right));
            int sum = l + r + root.val;
            ans = Math.max(ans, sum);
            return Math.max(l, r) + root.val;
        }
    }
    

      

    Approach #3: Python.

    # Definition for a binary tree node.
    # class TreeNode(object):
    #     def __init__(self, x):
    #         self.val = x
    #         self.left = None
    #         self.right = None
    
    class Solution(object):
        def solve(self, root):
            if not root: return 0
            l = max(0, self.solve(root.left))
            r = max(0, self.solve(root.right))
            self.ans = max(self.ans, l + r + root.val)
            return max(l, r) + root.val
        
        def maxPathSum(self, root):
            """
            :type root: TreeNode
            :rtype: int
            """
            if root == None:
                return 0
            self.ans = -sys.maxint
            self.solve(root)
            return self.ans
    

      

    永远渴望,大智若愚(stay hungry, stay foolish)
  • 相关阅读:
    Luogu P5030 长脖子鹿放置(网络流)
    BZOJ3037 创世纪(基环树DP)
    LuoguP1240 诸侯安置
    LuoguP3128 [USACO15DEC]最大流Max Flow (树上差分)
    总结-一本通提高篇&算竞进阶记录
    LuoguP5022 旅行 (割点,基环树)
    $tsinsenA1067$
    $SCOJ4427 Miss Zhao's Graph$
    $Edmonds-Karp$[网络流]
    $AC自动机$
  • 原文地址:https://www.cnblogs.com/h-hkai/p/10095698.html
Copyright © 2011-2022 走看看