python 递归
平衡二叉树
http://www.nowcoder.com/questionTerminal/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): def dfs(root): if root is None: return True, 0 lf, ld = dfs(root.left) rf, rd = dfs(root.right) return lf and rf and abs(ld-rd) <= 1, max(ld, rd)+1 flag, _ = dfs(pRoot) return flag