-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path110.balanced-binary-tree.py
53 lines (44 loc) · 1.29 KB
/
110.balanced-binary-tree.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
#
# @lc app=leetcode id=110 lang=python3
#
# [110] Balanced Binary Tree
#
# @lc code=start
# Definition for a binary tree node.
class TreeNode:
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right
import re
class Solution:
def height(self, node):
if not node:
return 0
return max(self.height(node.right), self.height(node.left)) + 1
def isBalanced(self, root):
if not root:
return True
if abs(self.height(root.left) - self.height(root.right)) > 1:
return False
else:
return self.isBalanced(root.left) and self.isBalanced(root.right)
def isBalanced2(self, root):
def check(node):
if not node:
return False
left = check(node.left)
right = check(node.right)
if not( left or right ) or abs(left - right) > 1:
return False
return max(left, right) + 1
return check(root) != -1
# @lc code=end
a = TreeNode(3)
b = TreeNode(9, left=None, right=None)
e = TreeNode(9, left=None, right=None)
c = TreeNode(-10, left=e)
d = TreeNode(20, left=c, right=a)
root = TreeNode(3, left=b, right=d)
pro = Solution()
print(pro.isBalanced2(root))