-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBalancedBinaryTree.py
More file actions
53 lines (47 loc) · 1.34 KB
/
Copy pathBalancedBinaryTree.py
File metadata and controls
53 lines (47 loc) · 1.34 KB
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
"""
@Project: leetcode
@file: BalancedBinaryTree.py
@author: AC
@time: 2016/5/7 22:49
@Description: Given a binary tree, determine if it is height-balanced.
For this problem, a height-balanced binary tree is defined as a binary tree in which
the depth of the two subtrees of every node never differ by more than 1.
"""
# Definition for a binary tree node.
class TreeNode(object):
def __init__(self, x):
self.val = x
self.left = None
self.right = None
class Solution(object):
def height(self, root):
if root is None:
return -1
else:
return 1+max(self.height(root.left), self.height(root.right))
def isBalanced(self, root):
"""
:type root: TreeNode
:rtype: bool
"""
if root is None:
return True
else:
lh = self.height(root.left)
rh = self.height(root.right)
if abs(lh-rh) <= 1:
return self.isBalanced(root.left) and self.isBalanced(root.right)
else:
return False
s = Solution()
nodes = []
for i in range(6):
nodes.append(TreeNode(i))
nodes[0].left = nodes[1]
nodes[0].right = nodes[2]
nodes[1].left = nodes[3]
nodes[1].right = nodes[4]
nodes[3].left = nodes[5]
for i in range(6):
print s.height(nodes[i])
print s.isBalanced(nodes[0])