zoukankan      html  css  js  c++  java
  • [LeetCode#124]Binary Tree Maximum Path Sum

    The problem:

    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.

    My analysis:

    This problem could be classified as a different kind of recursion problems.
    The classfication: from bottom to surface, without using value(except the answer set, and cur_root information) from previous level.
    To solve this problem: we could only make decision from post level(it is a very normal pattern to use recursion on binary tree).
    At each recursion level, we need following thinking:
    1. compute the longest(max) path for left-sub tree.
    2. compute the longest(max) path for right-sub tree.
    3. compute the maximum sum for current node.

    3.1 iff longest(left) > 0, we include it. 
    3.2 iff longest(right) > 0, we include it.

    4. return the longest path for current node. this part should be treated very carefully, it's different from the maximum sum we want to compute. we should only choose include either left longest path, right longest path or only the current node itself.

    return cur_root.val + Math.max(left_max, Math.max(0, right_max));

    5. One very important thing. Since the problem allows the path to start from any node, we should keep a global max value we have found out. Not only the value at root.(the root node only contain information from it)

    My solution:

    public class Solution {
        public int maxPathSum(TreeNode root) {
            
            if(root == null)
                return 0;
            
            ArrayList<Integer> max = new ArrayList<Integer>();
            max.add(Integer.MIN_VALUE);
            
            helper(root, max);
            
            return max.get(0);
        }
        
        private int helper(TreeNode cur_root, ArrayList<Integer> max) {
            
            if (cur_root == null)
                return 0;
            
            int left_max = helper(cur_root.left, max);
            int right_max = helper(cur_root.right, max);
            int cur_max = cur_root.val;
    
            if (left_max > 0)
                cur_max += left_max;
            
            if (right_max > 0)
                cur_max += right_max;
            
            if (cur_max > max.get(0))
                max.set(0, cur_max);
            
            return cur_root.val + Math.max(left_max, Math.max(0, right_max)); //a little tricky skill 
        }
    }
  • 相关阅读:
    Cocos2d JS 之消灭星星(六) 创建星星类
    Cocos2d JS 之消灭星星(五) 游戏主场景
    Cocos2d JS 之消灭星星(四) 游戏主场景顶部显示
    x1 carbon 扩展屏 模糊
    Linux MTD (Memory Technology Device) subsystem analysis -For Atheros char device
    putty 配置
    给Ubuntu更换成163的源(sources.list)Unable to locate package
    有道显示网络已断开
    Linux kernel 内核学习路线
    make only output error/warning message( 编译时,只输出错误信息和警告信息)
  • 原文地址:https://www.cnblogs.com/airwindow/p/4215984.html
Copyright © 2011-2022 走看看