-
Notifications
You must be signed in to change notification settings - Fork 0
/
55-二叉树的深度.py
52 lines (44 loc) · 1.64 KB
/
55-二叉树的深度.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
# -*- coding: utf-8 -*-
"""
Created on Mon Jul 22 16:06:33 2019
@author: Zzj
"""
# 55. 二叉树的深度
from BinaryTree import TreeNode
class Solution1:
# 输入一棵二叉树的根节点 求该树的深度 从根到叶节点路径最长的为树的深度
def TreeDepth(self, pRoot):
if not pRoot:
return 0
left = self.TreeDepth(pRoot.left)
right = self.TreeDepth(pRoot.right)
return max(left, right) + 1
class Solution2:
# 平衡二叉树, 输入一棵二叉树的节点,判断该树是不是平衡二叉树。
# 如果该树中任意节点的左、右子树的深度相差不超过1, 就是平衡二叉树
def TreeDepth(self, pRoot):
if not pRoot:
return 0
left = self.TreeDepth(pRoot.left)
right = self.TreeDepth(pRoot.right)
return max(left, right) + 1
def IsBalancedTree(self, pRoot):
if not pRoot:
return True
left = self.TreeDepth(pRoot.left)
right = self.TreeDepth(pRoot.right)
diff = abs(left - right)
if diff > 1:
return False
return self.IsBalancedTree(pRoot.left) and self.IsBalancedTree(pRoot.right)
if __name__ == "__main__":
node0 = TreeNode(8)
node1 = TreeNode(7, left=node0)
node2 = TreeNode(4)
node3 = TreeNode(5, left=node1)
node4 = TreeNode(6)
node5 = TreeNode(2, left=node2, right=node3)
node6 = TreeNode(3, right=node4)
root = TreeNode(1, left=node5, right=node6)
print(Solution1().TreeDepth(root))
print(Solution2().IsBalancedTree(root))