110. Balanced Binary Tree
题目:
https://leetcode.com/problems/balanced-binary-tree/
难度:
Easy
全程递归中
class Solution(object):
def isBalanced(self, root):
"""
:type root: TreeNode
:rtype: bool
"""
if root == None:
return True
lh = self.height(root.left)
rh = self.height(root.right)
if abs(lh-rh) <= 1 and self.isBalanced(root.left) and self.isBalanced(root.right):
return True
return False
def height(self, node):
if node == None:
return 0
return 1 + max(self.height(node.left),self.height(node.right))
网友评论