-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
判断平衡二叉树.py
58 lines (49 loc) · 1.41 KB
/
判断平衡二叉树.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
'''
输入一棵二叉树,判断该二叉树是否是平衡二叉树。
'''
# -*- coding:utf-8 -*-
class TreeNode:
def __init__(self, x):
self.val = x
self.left = None
self.right = None
class Solution:
def __init__(self):
self.flag = True
def IsBalanced_Solution(self, pRoot):
self.getDepth(pRoot)
return self.flag
def getDepth(self, pRoot):
if pRoot == None:
return 0
left = 1 + self.getDepth(pRoot.left)
right = 1 + self.getDepth(pRoot.right)
if abs(left - right) > 1:
self.flag = False
return left if left > right else right
class Solution2:
def getDepth(self, pRoot):
if pRoot == None:
return 0
return max(self.getDepth(pRoot.left), self.getDepth(pRoot.right)) + 1
def IsBalanced_Solution(self, pRoot):
if pRoot == None:
return True
if abs(self.getDepth(pRoot.left)-self.getDepth(pRoot.right)) > 1:
return False
return self.IsBalanced_Solution(pRoot.left) and self.IsBalanced_Solution(pRoot.right)
pNode1 = TreeNode(1)
pNode2 = TreeNode(2)
pNode3 = TreeNode(3)
pNode4 = TreeNode(4)
pNode5 = TreeNode(5)
pNode6 = TreeNode(6)
pNode7 = TreeNode(7)
pNode1.left = pNode2
pNode1.right = pNode3
pNode2.left = pNode4
pNode2.right = pNode5
pNode3.right = pNode6
pNode5.left = pNode7
S = Solution2()
print(S.getDepth(pNode1))