[
tree
bfs
dfs
]
Leetcode 0958. Check Completeness of a Binary Tree
Problem statement
https://leetcode.com/problems/check-completeness-of-a-binary-tree/
Solution
We can traverse our trie using bfs, and when the first time we meet None
in our queue we need to make sure that all elements in our queue are None
.
Complexity
It is O(n)
for time and O(w)
for space.
Code
class Solution:
def isCompleteTree(self, root):
queue = deque([root])
while queue:
node = queue.popleft()
if not node: return all(i == None for i in queue)
queue.append(node.left)
queue.append(node.right)
return True