Leetcode - Binary Tree Maximum Path Sum

My code:

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    public int maxPathSum(TreeNode root) {
        int[] max = new int[1];
        max[0] = Integer.MIN_VALUE;
        findMax(root, max);
        return max[0];
    }
    
    private int findMax(TreeNode root, int[] max) {
        if (root == null)
            return 0;
        int left = findMax(root.left, max);
        int right = findMax(root.right, max);
        int curr = Math.max(root.val, Math.max(root.val + left, root.val + right));
        max[0] = Math.max(max[0], Math.max(curr, left + root.val + right));
        return curr;
    }
}

My test result:

Paste_Image.png

這道題目需要仔細分析。而我沒有怎么分析,就直接開始做了。等到認識到這道題目復雜性的時候,腦子已經亂了,所以網上看了答案,才寫出來。

首先,針對上篇文章,我說的,除了容器,其他的,形參是無法改變實參的。
所以在C++中發明了引用,可以通過形參改變實參。但是Java中沒有啊,怎么辦。
只能傳入容器,然后修改容器中的值,從而達到這個效果。
比如這道題目,如果光傳入一個 int max; 是毫無作用的,
所以,傳入了 int[] max = new int[1];
里面只放一個元素,但是他是存放在容器中的,他的改變,可以影響到外面的實參。
這個辦法雖然笨,但是的確好用。
接下來說這道題目。
一個結點一共有四種取值方式,如下:

所以,必須采用bottom-up 的做法,先遞歸,再總結,post-order
pre-order -> top-down
post-order -> bottom-up
in-order -> ?
level-order -> bfs

從最底層開始,算出這四種方式的值,取最大值,然后更新進max[0]
同時,可以發現, #1, #2, #3 是屬于第一種類型的,也就是從一個結點出發往下走,起點總是root
而#4則是第二種類型,起點并不是root。
所以,上一層的結點,也就是1結點的父親節點,它是需要#1,#2,#3 這三種遍歷方式的最大值的。
如果你擔心,子節點的值,比如結點2的值,可能大于#1,但是傳入上層的,卻是#1.
那么這種擔心是多余的。
其實我們在做兩件事。
更新max, 如果結點2更大,那么,他的值早就被更新到max里面去了。
第二件事,就是不斷地給上層建筑提供第一種類型的后代結點取值。使得他可以進行屬于他的四種遍歷,然后以此類推。
整個算法很簡潔,不得不佩服。
下面提供參考的兩篇博文。
http://bangbingsyb.blogspot.com/2014/11/leetcode-binary-tree-maximum-path-sum.html
http://www.programcreek.com/2013/02/leetcode-binary-tree-maximum-path-sum-java/

另外,這道題目的BST條件,似乎沒什么用。因為如果全是負數,BST也沒幫助了。
**
總結:
pre-order -> top-down
post-order -> bottom-up
in-order -> ?
level-order -> bfs

做一道題目,尤其是hard,一定要事先分析好,他有幾種變化形式!
**

Anyway, Good luck, Richardo!

My code:

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

沒能一遍過。有點浮躁。雖然一開始思路是對的。
對于 到某個結點為止的最大值,可以是

  1. 結點本身
  2. 結點 + dfs(left child)
  3. 結點 + dfs(right child)
  4. 結點 + dfs(left child) + dfs(right child)
    這里可以找出最大值。
    然后,返回的時候,這里我犯錯了。
    他必須是一條路徑,所以只能返回 1 2 3 情況下的最大值。

這道題目讓我想起了兩道題目,

  1. Maximum Product Subarray -
    http://www.lxweimin.com/p/e1456b90c819

  2. Maximum Subarray -
    http://www.lxweimin.com/p/ac172786c4ab

他們也是,當前的最大值,

  1. 當前值
  2. 當前值 + 過去值
  3. 過去值

返回的是情況1 2 下的最大值。這樣才能保證連續。

Anyway, Good luck, Richardo!

My code:

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    private int max = Integer.MIN_VALUE;
    public int maxPathSum(TreeNode root) {
        if (root == null) {
            return 0;
        }
        
        int ret = helper(root);
        max = Math.max(max, ret);
        return max;
    }
    
    private int helper(TreeNode root) {
        if (root.left == null && root.right == null) {
            max = Math.max(max, root.val);
            return root.val;
        }
        else if (root.left == null || root.right == null) {
            int temp = (root.left == null ? helper(root.right) : helper(root.left));
            max = Math.max(max, Math.max(temp, root.val));
            return Math.max(root.val, temp + root.val);
        }
        else {
            int left= helper(root.left);
            int right = helper(root.right);
            max = Math.max(max, Math.max(left + root.val + right, Math.max(root.val, Math.max(left, right))));
            return Math.max(root.val, Math.max(left, right) + root.val); 
        }
    }
}

做了半小時才做出來。還是考慮的太不仔細了。
然后有些情況完全沒必要重復比較。

Anyway, Good luck, Richardo! -- 08/28/2016

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容