Minimum Subtree

Posted codingEskimo

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
     */
    private int minSum = Integer.MAX_VALUE;
    private TreeNode minNode = null;
    public TreeNode findSubtree(TreeNode root) {
        // Write your code here
        findMin(root);
        return minNode;
    }
    
    private int findMin(TreeNode root) {
        if (root == null) {
            return 0;
        }
        
        int left = findMin(root.left);
        int right = findMin(root.right);
        
        int sum = left + right + root.val;
        if (sum < minSum) {
            minSum = sum;
            minNode = root;
        }
        return sum;
    }
}

 

以上是关于Minimum Subtree的主要内容,如果未能解决你的问题,请参考以下文章

lintcode596- Minimum Subtree- easy

[CF893F]Subtree Minimum Query (主席树)

CF893F Subtree Minimum query 主席树

CF893F Subtree Minimum Query 主席树

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

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