Minimum Subtree

Posted yunyouhua

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Minimum Subtree相关的知识,希望对你有一定的参考价值。

我的错误代码

理递归思路

技术分享
/**
 * 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的主要内容,如果未能解决你的问题,请参考以下文章

lintcode596- Minimum Subtree- easy

[CF893F]Subtree Minimum Query (主席树)

CF893F Subtree Minimum query 主席树

CF893F Subtree Minimum Query 主席树

[Git] 使用 Git SubTree 来共享函示库源代码

golang项目git-subtree完美解决差异包管理