Widest Level of a Binary Tree
Updated
Problem statement
Given the root of a binary tree, return the width of its widest level.
To measure a level, number the spots of the tree as in a complete binary tree: the root is at position 1, and the spot at position p has two child spots, at 2p and 2p + 1. This holds for every spot above the level, empty or not, so an empty spot still has two (empty) child spots below it. The width of a level is the position of its rightmost node minus the position of its leftmost node, plus 1. So empty spots between two nodes of the level count, but empty spots outside them do not. The node values do not affect the answer.
Function
widestLevel(root: TreeNode) → intExamples
Example 1
root = [4,2,7,1,null,null,9]return = 4Level 2 holds 1, then two empty spots (the right child of 2 and the left child of 7), then 9. Counting from 1 to 9 with the empty spots gives a width of 4, the largest level. The answer is 4.
Example 2
root = [4,2,7,1,3,6]return = 3Level 1 has width 2. Level 2 holds 1, 3 and 6 side by side, a width of 3; the missing right child of 7 is after the last node, so it is not counted. The answer is 3.
Example 3
root = [5,null,8,null,6]return = 1Every level has only one node, so every width is 1.
Example 4
root = [0,-20,20,null,-10,10,null,-15,null,null,15,null,-12,12]return = 6Positions: 0 at 1; -20 at 2 and 20 at 3; -10 at 5 and 10 at 6; -15 at 10 and 15 at 13; -12 at 21 and 12 at 26. The empty spots 11 and 12 on level 3 still have child spots (22, 23, 24 and 25), and those sit between -12 and 12. So the bottom level has width 26 - 21 + 1 = 6. The level widths are 1, 2, 2, 4 and 6, so the answer is 6.
Constraints
- The tree has
1to1000nodes. -100 <= Node.val <= 100- The answer is at most
2^31 - 1.
More Adobe Phone Screen questions
- Evaluate a Parenthesized Integer ExpressionPHONE SCREEN · Seen Oct 2026
- All Elements of Two BSTs in OrderPHONE SCREEN · Seen Aug 2026
- Rotate an Array In PlaceOA · PHONE SCREEN · Seen Aug 2026
- Binary Tree Level-Order Traversal by LevelsPHONE SCREEN · ONSITE INTERVIEW · Seen Aug 2026
- Count Occurrences in a Sorted ArrayPHONE SCREEN · ONSITE INTERVIEW · Seen Jul 2026
- Count Pairs with Absolute Difference KPHONE SCREEN · Seen Jul 2026
- Count Subsets with Sum KPHONE SCREEN · Seen Jul 2026
- Numbers with Exactly Three DivisorsPHONE SCREEN · Seen Jul 2026
- Sum of Even Nodes on a Path in a Numbered Binary TreePHONE SCREEN · Seen Jul 2026
- Largest Value in Every Sliding WindowPHONE SCREEN · Seen Jul 2026
Similar questions at other companies
- Maximum-Scoring Consecutive Tree LevelsJane Street · PHONE SCREENJane Street interview questions
- Minimum Edits to a Perfect Binary TreeSnowflake · PHONE SCREENSnowflake interview questions
- Find All Paths for a Name in a File SystemThe Walt Disney Company · PHONE SCREENThe Walt Disney Company interview questions
- Binary Tree Spiral Level OrderAmazon · PHONE SCREEN · ONSITE INTERVIEW
- Check a Complete Binary TreeGoldman Sachs · PHONE SCREENGoldman Sachs interview questions
- Binary Tree Right Side ViewGoogle · PHONE SCREEN · ONSITE INTERVIEWGoogle interview questions
- Same Binary TreeOdoo · PHONE SCREENOdoo interview questions
- Binary Tree Zigzag Level Order TraversalLinkedIn · PHONE SCREEN · ONSITE INTERVIEWLinkedIn interview questions
- Insert an Employee into an Organization HierarchyWaymo · PHONE SCREENWaymo interview questions
- Bottom View of a Binary TreeOracle · PHONE SCREEN · ONSITE INTERVIEWOracle interview questions
Interview reports that mention this question
Adobe interview experiences
- Adobe Web, QA, DevOps, ML Roles: 13 Experiences (Part 3 of 3)
- Adobe SWE Interviews, Level Unknown: 25 Experiences (Part 2 of 3)
- Adobe Interviews, Level Unknown: 38 Experiences (Part 1 of 3)
- Adobe New Grad MTS Interview Questions: 7 Experiences
- Adobe ML/Research Intern Interviews: 12 Experiences (Part 4 of 4)