码迷,mamicode.com
首页 > 其他好文 > 详细

Minimum Subtree

时间:2017-04-18 23:41:53      阅读:190      评论:0      收藏:0      [点我收藏+]

标签:tty   blog   pre   amp   minimum   int   imu   class   value   

我的错误代码

理递归思路

技术分享
/**
 * Definition of TreeNode:
 * public class TreeNode {
 *     public int val;
 *     public TreeNode left, right;
 *     public TreeNode(int val) {
 *         this.val = val;
 *         this.left = this.right = null;
 *     }
 * }
 */
public class Solution {
    /**
     * @param root the root of binary tree
     * @return the root of the minimum subtree
     */
    public TreeNode findSubtree(TreeNode root) {
        // Write your code here
        TreeNode result = new TreeNode(0);
        int min=0;
        if (root == null) {
            return null;
        }
  
        findSum(root, result, min);
        
        return result;
        
    }
    private int findSum(TreeNode root, TreeNode result, int min) {
        if (root == null) {
            return 0;
        }
        int sum;
        int left = findSum(root.left, result, min);
        int right = findSum(root.right, result, min);
        sum = root.val + left + right;
        
        if (root.val + left + right > left && left <= right) {
            min = left;
            result = root.left;
        } else if (root.val + left + right > right && left >= right) {
            min = right;
            result = root.right;
        } else {
            min = root.val + left + right;
            result = root;
        }
        return sum;
    }
}
View Code

九章

技术分享
// version 1 : traverse + divide conquer
public class Solution {
    private TreeNode subtree = null;
    private int subtreeSum = Integer.MAX_VALUE;
    /**
     * @param root the root of binary tree
     * @return the root of the minimum subtree
     */
    public TreeNode findSubtree(TreeNode root) {
        helper(root);
        return subtree;
    }
    
    private int helper(TreeNode root) {
        if (root == null) {
            return 0;
        }
        
        int sum = helper(root.left) + helper(root.right) + root.val;
        if (sum < subtreeSum) {
            subtreeSum = sum;
            subtree = root;
        }
        return sum;
    }
}

// version 2: Pure divide conquer
class ResultType {
    public TreeNode minSubtree;
    public int sum, minSum;
    public ResultType(TreeNode minSubtree, int minSum, int sum) {
        this.minSubtree = minSubtree;
        this.minSum = minSum;
        this.sum = sum;
    }
}

public class Solution {
    /**
     * @param root the root of binary tree
     * @return the root of the minimum subtree
     */
    public TreeNode findSubtree(TreeNode root) {
        ResultType result = helper(root);
        return result.minSubtree;
    }
    
    public ResultType helper(TreeNode node) {
        if (node == null) {
            return new ResultType(null, Integer.MAX_VALUE, 0);
        }
        
        ResultType leftResult = helper(node.left);
        ResultType rightResult = helper(node.right);
        
        ResultType result = new ResultType(
            node,
            leftResult.sum + rightResult.sum + node.val,
            leftResult.sum + rightResult.sum + node.val
        );
        
        if (leftResult.minSum < result.minSum) {
            result.minSum = leftResult.minSum;
            result.minSubtree = leftResult.minSubtree;
        }
        
        if (rightResult.minSum < result.minSum) {
            result.minSum = rightResult.minSum;
            result.minSubtree = rightResult.minSubtree;
        }
        
        return result;
    }
}
View Code

 

Minimum Subtree

标签:tty   blog   pre   amp   minimum   int   imu   class   value   

原文地址:http://www.cnblogs.com/yunyouhua/p/6731086.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!