bugfree Icon
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course

Leetcode Problem 958. Check Completeness of a Binary Tree

958. Check Completeness of a Binary Tree

Leetcode Solutions

Breadth First Search (BFS) Approach

  1. If the root is null, return true as an empty tree is a complete binary tree.
  2. Initialize a queue and add the root to it.
  3. Initialize a boolean flag nullNodeFound to false.
  4. While the queue is not empty: a. Dequeue the front node from the queue. b. If the node is null, set nullNodeFound to true. c. If the node is not null: i. If nullNodeFound is true, return false as we have found a non-null node after a null node. ii. Otherwise, enqueue the left and right children of the node.
  5. If the loop completes without returning false, return true.
UML Thumbnail

Depth First Search (DFS) Approach

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...
bugfree Icon
OR