Balanced binary tree binary tree _
Description Title
input binary tree, the binary tree is determined whether a balanced binary tree.
Answers
# -*- 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
if not pRoot:
return True
left_depth = self.Treedepth(pRoot.left)
right_depth = self.Treedepth(pRoot.right)
if abs(left_depth - right_depth)>1:
return False
return self.IsBalanced_Solution(pRoot.left) and self.IsBalanced_Solution(pRoot.right)
def Treedepth(self, pRoot):
if not pRoot:
return 0
left = self.Treedepth(pRoot.left) + 1
right = self.Treedepth(pRoot.right) + 1
return max(left,right)