FastPrepGroup Identical Tree Levels
Problems/Group Identical Tree Levels

Group Identical Tree Levels

Salesforce logoSalesforce● MediumFULLTIMEONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: This comes from the first of three technical rounds in a Salesforce SMTS loop in Bangalore. The coding task was about binary tree levels: find the levels that hold exactly the same sequence of values, and show which of them repeat.

The post does not share the test case, the output format or any limits. Here the answer lists the level numbers of each repeated group, levels count from 0 at the root, groups are ordered by their first level, and the tree has at most 5000 nodes with values from -1000 to 1000.

The judged task matches the post at about 85%.

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]]
root
511722

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]]
root
33434

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 = []
root
12332

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 1 and 5000.
  • -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

Salesforce interview experiences

  • Salesforce Infrastructure SWE OA and Onsite: 1 Experience
  • Salesforce Experienced Backend SWE First Round: 1 Experience
All Salesforce interview questionsSee Salesforce hiring insights
Saved
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public List<List<Integer>> groupIdenticalLevels(TreeNode root) {
        // Write your code here.
    }
}
Checking account…