-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCheck Completeness of a Binary Tree.py
68 lines (47 loc) · 2.39 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
# https://leetcode.com/problems/check-completeness-of-a-binary-tree/
# 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
from collections import deque
class Solution:
def isCompleteTree(self, root: Optional[TreeNode]) -> bool:
"""
OBJECTIVE: Return true if given binary tree is complete. If not, return false
NOTE: A complete binary tree only has NULL nodes at the bottom, far-right side of the tree. I.e, if a tree has N nodes, then N - 1 nodes can't be null!
Time Complexity: O(n) where n = # of nodes because the tree is being traversed once through breadth-first search
Space Complexity: O(n) where n = # of nodes because all the nodes are being added to a queue during a breadth-first search
"""
# If root is empty or by itself, return true
if root is None or (root.left == root.right == None):
return True
# Use bfs() to traverse tree
queue = deque([root])
discoveredNull = False # <= Create a variable to determine if a None node was discovered
while queue:
# Pop first element from queue
popped = queue.popleft()
# Check if there's a left child
if popped.left:
# If a null node was previously discovered, return false
if discoveredNull:
return False
# Add left child to queue
queue.append(popped.left)
# If left child is null, set boolean variable to true
else:
discoveredNull = True
# Check if there's a right child
if popped.right:
# If a null node was previously discovered, return false
if discoveredNull:
return False
# Add right child to queue
queue.append(popped.right)
# If right child is null, set boolean variable to true
else:
discoveredNull = True
# If function is still continuing, then tree is complete
return True