• 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)
  • 相关阅读:
    需要学习的技术
    Building wheel for uwsgi (setup.py) ... error 解决问题
    version `ZLIB_1.2.3.4‘ not found 解决方法
    module 'tensorflow' has no attribute 'space_to_depth'(已解决)
    python语法—命名元祖、偏函数
    python—set集合比较(交集、并集,差集)
    websocket接口测试
    linux根目录扩容方法
    django—问题—中文编码格式报错 、分页warning
    python—使用sorted对字典进行排序
  • 原文地址:https://www.cnblogs.com/h-hkai/p/10095698.html
Copyright © 2020-2023  润新知