首页 > 代码库 > Minimum Subtree
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
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。