# 37、数字在排序数组中出现的次数

# -*- coding:utf-8 -*-
class Solution:
def GetNumberOfK(self, data, k):
# write code here
count = 0
for i in range(len(data)):
if data[i]==k:
count+=1
return count

# 38、二叉树的深度

# -*- coding:utf-8 -*-
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None
class Solution:
def TreeDepth(self, pRoot):
# write code here
if pRoot is None:
return 0
left = self.TreeDepth(pRoot.left)
right = self.TreeDepth(pRoot.right)
depth = max(left,right)+1
return depth

# 39、平衡二叉树

# -*- coding:utf-8 -*-
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None
class Solution:
res=True
def IsBalanced_Solution(self, pRoot):
# write code here
self.TreeDepth(pRoot)
return self.res
#这一部分采用38题的判断树的深度的函数。
def TreeDepth(self, Root):
# write code here
if Root is None:
return 0
# 判断已经有子树不是平衡二叉树，终止该次递归
if not self.res:
return 1
left = self.TreeDepth(Root.left)+1
right = self.TreeDepth(Root.right)+1
if abs(left-right)>1:
self.res = False
return max(left,right)
#不要每个节点都去求一次高度，避免重复
08-29 1399
09-10 3653

06-02 2833
08-16 4万+
11-02 5万+
12-19
08-05 1万+