大佬们啊
平衡二叉树
http://www.nowcoder.com/questionTerminal/8b3b95850edb4115918ecebdf1b4d222
大佬们给看一下,为什么这个就是不通过啊,我的思想是统计每个叶子节点的层次深度,通过率百分之14 左右
class Solution:
# write code here
depths=[]
def dfs(self,pRoot,depth):
if pRoot is None:
#this is leaf node
self.depths.append(depth)
return
self.dfs(pRoot.left,depth+1)
self.dfs(pRoot.right,depth+1)
def IsBalanced_Solution(self, pRoot):
"""
determine if a tree is an equilibrium binary tree
need dfs
:param pRoot:
:return:
"""
self.dfs(pRoot,0)
#determine the list of depth
min_ = self.depths[0]
max_ = self.depths[0]
for item in self.depths:
min_=min(min_,item)
max_=max(max_,item)
if max_-min_>=2:
return False
else:
return True