Group Identical Tree Levels
Updated
Problem statement
You are given the root of a binary tree. The root is on level 0, its children are on level 1, and so on. The sequence of a level is the list of its node values read from left to right.
Two levels are identical when their sequences are equal: they have the same number of nodes and the same value at every position. Group the levels so that identical levels are in the same group.
Return only the groups that contain at least two levels. Each group is the list of its level numbers in increasing order, and the groups are ordered by their smallest level number. If no two levels are identical, return an empty list.
Function
groupIdenticalLevels(root: TreeNode) → List<List<Integer>>Examples
Example 1
root = [5,1,2,1,null,null,2,7]return = [[1,2]]The level sequences are [5], [1, 2], [1, 2] and [7]. Levels 1 and 2 are identical, and no other level repeats, so the answer is [[1, 2]].
Example 2
root = [3,3,null,4,null,3,null,4]return = [[0,1,3],[2,4]]Each level has one node. The sequences are [3], [3], [4], [3] and [4]. Levels 0, 1 and 3 form one group, and levels 2 and 4 form another. The group containing level 0 comes first.
Example 3
root = [1,2,3,3,null,null,2]return = []The sequences are [1], [2, 3] and [3, 2]. Levels 1 and 2 hold the same values in a different order, so they are not identical, and the answer is empty.
Constraints
- The number of nodes in the tree is between
1and5000. -1000 <= Node.val <= 1000
More Salesforce Onsite Interview questions
- Count Increasing TripletsONSITE INTERVIEW · Seen Nov 2026
- Longest Substring Without Repeating CharactersONSITE INTERVIEW · Seen Nov 2026
- Reverse a Sublist of a Linked ListONSITE INTERVIEW · Seen Nov 2026
- Rotting OrangesONSITE INTERVIEW · Seen Nov 2026
- 3Sum SmallerONSITE INTERVIEW · Seen Nov 2026
- Count Zero Request ServersONSITE INTERVIEW · Seen Nov 2026
- Meeting Rooms IIONSITE INTERVIEW · Seen Oct 2026
- Construct a Tree from Level-Order and Inorder TraversalsONSITE INTERVIEW · Seen Sep 2026
- Maximize Monsters DefeatedONSITE INTERVIEW · Seen Sep 2026
- Flatten Nested Map Paths with a StackONSITE INTERVIEW · Seen Sep 2026
Similar questions at other companies
- Delete Nodes and Return Forest RootsGoogle · ONSITE INTERVIEWGoogle interview questions
- Bottom View of a Binary TreeOracle · PHONE SCREEN · ONSITE INTERVIEWOracle interview questions
- Vertical Order Traversal of a Binary TreeAmazon · ONSITE INTERVIEW
- Binary Tree Vertical Order TraversalBloomberg LP · PHONE SCREEN · ONSITE INTERVIEWBloomberg LP interview questions
- Binary Tree Vertical Order with Stable TiesMeta · ONSITE INTERVIEWMeta interview questions
- Top View of a Binary TreeInMobi · ONSITE INTERVIEWInMobi interview questions
- Count LevelsBlackRock · OABlackRock interview questions
- Insert an Employee into an Organization HierarchyWaymo · PHONE SCREENWaymo interview questions
- Everyone Under a ManagerAdobe · PHONE SCREENAdobe interview questions
- Team Cost Under a ManagerRippling · PHONE SCREENRippling interview questions