题解 | #平衡二叉树#
平衡二叉树
http://www.nowcoder.com/practice/8b3b95850edb4115918ecebdf1b4d222
# -*- coding:utf-8 -*- # class TreeNode: # def __init__(self, x): # self.val = x # self.left = None # self.right = None class Solution: def IsBalanced_Solution(self, pRoot): # write code here def helper(root): if not root: return 0 ldep = helper(root.left) rdep = helper(root.right) sub = abs(ldep-rdep) if ldep == -1 or rdep == -1: return -1 if sub > 1: return -1 else: return max(ldep, rdep) + 1 return helper(pRoot) != -1