
Problem
Solution
Submissions
Complete Binary Tree
Certification: Intermediate Level
Accuracy: 0%
Submissions: 0
Points: 10
Write a C# program to determine if a given binary tree is a complete binary tree. A complete binary tree is a binary tree in which every level, except possibly the last, is completely filled, and all nodes in the last level are as far left as possible.
Example 1
- Input:
- Output: true
- Explanation:
- Every level is filled except the last level, and all nodes in the last level are as far left as possible.
Example 2
- Input:
- Output: false
- Explanation:
- The last level is not filled from left to right. Node 7 is on the right side without having a left sibling.
Constraints
- The number of nodes in the tree is between 1 and 10^4
- -1000 ≤ Node.val ≤ 1000
- Time Complexity: O(n) where n is the number of nodes
- Space Complexity: O(n) for the queue in a level-order traversal
Editorial
My Submissions
All Solutions
| Lang | Status | Date | Code |
|---|---|---|---|
| You do not have any submissions for this problem. | |||
| User | Lang | Status | Date | Code |
|---|---|---|---|---|
| No submissions found. | ||||
Solution Hints
- Use a level-order traversal (BFS) to check if the tree is complete.
- Keep track of whether we've seen a non-full node (a node with missing children).
- If we ever encounter a node after seeing a non-full node, the tree is not complete.
- A non-full node is one that either has no left child or has a left child but no right child.
- All nodes after a non-full node should be leaf nodes.