-
Notifications
You must be signed in to change notification settings - Fork 43
/
check-completeness-of-a-binary-tree.py
177 lines (168 loc) · 5.24 KB
/
check-completeness-of-a-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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# V0
from collections import deque
class Solution:
def isCompleteTree(self, root):
if not root:
return True
q= deque([root])
no_sub_node = False
while q:
node = q.popleft()
if not node:
no_sub_node = True
else:
if no_sub_node:
return False
q.append(node.left)
q.append(node.right)
return True
# V1
# https://blog.csdn.net/fuxuemingzhu/article/details/85032299
# IDEA : BFS
# IDEA : USE BFS GO THROUGH TREE,
# IF THERE IS ANY "NONE" AT A NODE THAT HAS SUB TREE -> RETURN FALSE (NOT a completeness binary tree)
# ELSE : STILL a completeness binary tree
# DEFINITION : completeness binary tree
# Definition of a complete binary tree from Wikipedia:
# In a complete binary tree every level, except possibly the last, is completely filled, and all nodes in the last level are as far left as possible. It can have between 1 and 2h nodes inclusive at the last level h.
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
import collections
class Solution(object):
def isCompleteTree(self, root):
"""
:type root: TreeNode
:rtype: bool
"""
if not root: return True
res = []
que = collections.deque()
que.append(root)
hasNone = False
while que:
size = len(que)
for i in range(size):
node = que.popleft()
if not node: # if there is NO sub tree, then hasNone = True is acceptable (as a completeness binary tree)
hasNone = True
continue
if hasNone: # if there is NO sub tree, then hasNone = True is NON acceptable (NOT a completeness binary tree)
return False
que.append(node.left)
que.append(node.right)
return True
# V1'
# https://www.jiuzhang.com/solution/check-completeness-of-a-binary-tree/#tag-other-lang-python
# IDEA : BFS
from collections import deque
class Solution:
def isCompleteTree(self, root: TreeNode) -> bool:
if not root:
return True
queue = deque([root])
is_miss = False
while queue:
node = queue.popleft()
if not node:
is_miss = True
else:
if is_miss:
return False
queue.append(node.left)
queue.append(node.right)
return True
# V1''
# https://blog.csdn.net/fuxuemingzhu/article/details/85032299
# IDEA : DFS
# 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 isCompleteTree(self, root):
"""
:type root: TreeNode
:rtype: bool
"""
if not root: return True
res = []
self.getlevel(res, 0, root)
depth = len(res) - 1
for d in range(depth):
if d != depth - 1:
if None in res[d] or len(res[d]) != (2 ** d):
return False
else:
ni = -1
for i, n in enumerate(res[d]):
if n == None:
if ni == -1:
ni = i
else:
if ni != -1:
return False
return True
def getlevel(self, res, level, root):
if level >= len(res):
res.append([])
if not root:
res[level].append(None)
else:
res[level].append(root.val)
self.getlevel(res, level + 1, root.left)
self.getlevel(res, level + 1, root.right)
# V2
# Time: O(n)
# Space: O(w)
# 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 isCompleteTree(self, root):
"""
:type root: TreeNode
:rtype: bool
"""
end = False
current = [root]
while current:
next_level = []
for node in current:
if not node:
end = True
continue
if end:
return False
next_level.append(node.left)
next_level.append(node.right)
current = next_level
return True
# Time: O(n)
# Space: O(w)
class Solution2(object):
def isCompleteTree(self, root):
"""
:type root: TreeNode
:rtype: bool
"""
prev_level, current = [], [(root, 1)]
count = 0
while current:
count += len(current)
next_level = []
for node, v in current:
if not node:
continue
next_level.append((node.left, 2*v))
next_level.append((node.right, 2*v+1))
prev_level, current = current, next_level
return prev_level[-1][1] == count