-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest55_二叉树的深度.py
38 lines (34 loc) · 1013 Bytes
/
test55_二叉树的深度.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#1 二叉树的深度
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
def TreeDepth(self, pRoot):
# write code here
#n = max(n左, n右) + 1
if not pRoot:
return 0
left = self.TreeDepth(pRoot.left)
right = self.TreeDepth(pRoot.right)
return max(left, right)+1
#2 平衡二叉树
# 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
return self.balanceHeight(pRoot) >= 0
def balanceHeight(self, root):
if not root:
return 0
left = self.balanceHeight(root.left)
right = self.balanceHeight(root.right)
if (left<0 or right<0 or abs(left-right)>1):
return -1
return max(left, right)+1