# Definition for a binary tree node # class TreeNode: # def __init__(self, x): # self.val = x # self.left = None # self.right = None class Solution: # @param root, a tree node # @return a boolean def subtreeMm(self, crt): if not crt: return True, 0 lsp,rsp=self.subtreeMm(crt.left),self.subtreeMm(crt.right) if lsp[0] and rsp[0]: return [True, 1+max(lsp[1],rsp[1])] if abs(lsp[1]-rsp[1])<=1 else [False,0] else: return False,0 def isBalanced(self, root): return self.subtreeMm(root)[0]
Sunday, September 7, 2014
Leetcode: Balanced Binary Tree @Python
Subscribe to:
Post Comments
(
Atom
)
No comments :
Post a Comment