首页 > 其他 > 详细

Minimum Subtree

时间:2017-05-31 09:42:00      阅读:332      评论:0      收藏:0      [点我收藏+]
/**
 * 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

原文:http://www.cnblogs.com/codingEskimo/p/6922491.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!