Ctrl K

Tree Packed to the Left

Medium
You are given the root of a binary tree. Check whether it is tightly packed: every level except possibly the last is completely filled, and the nodes of the last level sit side by side starting from the far left, with no gaps. Return True if the tree is packed this way and False otherwise.
Example 1
root=tree_node([7, 3, 9, 1, 4]) → True

The first two levels are full, and the two nodes on the last level take the leftmost spots.

Example 2
root=tree_node([7, 3, 9, None, 4]) → False

Node 3 has no left child but has a right one, so the last level has a gap on the left.

Example 3
root=tree_node([7, 3, 9, 1, 4, 2, 8, 5]) → True
Constraints
  • 1 ≤ number of nodes ≤ 100
  • 1 ≤ Node.val ≤ 1000
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsTree Rows by Depth MediumSnake Through the Tree Levels MediumTree Levels From the Bottom Up MediumThe Tree Seen From the Right MediumTree Columns, Top Down Medium
loading Python…
Input
root=tree_node([7, 3, 9, 1, 4])
Expected
True

Run checks the first 3 tests in your browser. Submit checks all 139 tests on the server, hidden ones included.