Minimum Subtree
Notice
"""
Definition of TreeNode:
class TreeNode:
def __init__(self, val):
this.val = val
this.left, this.right = None, None
"""
class Solution:
# @param {TreeNode} root the root of binary tree
# @return {TreeNode} the root of the minimum subtree
import sys
min_weight = sys.maxint
result = None
def findSubtree(self, root):
# Write your code here
self.mintree(root)
return self.result
def mintree(self, root):
if not root:
return 0
left = self.mintree(root.left)
right = self.mintree(root.right)
if left+right+root.val<=self.min_weight:
self.min_weight = left+right+root.val
self.result = root
return left+right+root.valLast updated