输入一棵二叉树,判断该二叉树是否是平衡二叉树。
# -*- 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
else
:
left
=
self
.TreeDepth(pRoot.left)
right
=
self
.TreeDepth(pRoot.right)
if
abs
(left
-
right)>
1
:
return
False
else
:
return
True
def
TreeDepth(
self
, pRoot):
if
pRoot:
left
=
self
.TreeDepth(pRoot.left)
+
1
right
=
self
.TreeDepth(pRoot.right)
+
1
maxL
=
max
(left,right)
return
maxL
else
:
return
0