题解 | #平衡二叉树#
平衡二叉树
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 maxdep(self,root):
if root==None :
return 0
return max(self.maxdep(root.left),self.maxdep(root.right))+1
def IsBalanced_Solution(self, pRoot):
# write code here
if pRoot is None:
return True
if abs(self.maxdep(pRoot.left)-self.maxdep(pRoot.right))>1:
return False
return self.IsBalanced_Solution(pRoot.left) and self.IsBalanced_Solution(pRoot.right)