Inorder Tree String Equivalence
Updated
Problem statement
Two binary trees store a string at every node. The text of a tree is obtained by visiting all nodes in inorder (left subtree, node, right subtree) and joining their labels with no separator. Return true if the two texts are identical, and false otherwise.
Tree representation
The first tree is described by labels1, left1, and right1; the second uses labels2, left2, and right2. For either tree:
- Node indices are
0throughn - 1. Whenn > 0, node0is the root. labels[i]is the string stored at nodei.left[i]andright[i]are its child indices;-1means that child is absent.- The child links form one valid binary tree containing every listed node. The node array is not an inorder traversal.
- All three arrays are empty for an empty tree, whose text is the empty string. An empty node label contributes no characters.
Memory follow-up
Solve the comparison without constructing either complete concatenated text or an array of all visited labels. Visit nodes in inorder as needed and compare their characters as you go. Labels can have different lengths: one node labeled "ab" may match two consecutive inorder labels "a" and "b". Different tree shapes and different label boundaries do not by themselves make the texts unequal. Aim for auxiliary space O(h1 + h2), where h1 and h2 are the tree heights.
Function
equivalentInorderText(labels1: String[], left1: int[], right1: int[], labels2: String[], left2: int[], right2: int[]) → booleanExamples
Example 1
labels1 = ["b","a","cd"]left1 = [1,-1,-1]right1 = [2,-1,-1]labels2 = ["ab","c","d"]left2 = [-1,-1,-1]right2 = [1,2,-1]return = trueThe first tree visits node indices 1, 0, 2, giving "a" + "b" + "cd" = "abcd". The second visits 0, 1, 2, giving "ab" + "c" + "d" = "abcd".
Example 2
labels1 = ["x","a","b"]left1 = [1,-1,-1]right1 = [2,-1,-1]labels2 = ["y","a","b"]left2 = [1,-1,-1]right2 = [2,-1,-1]return = falseThe texts are "axb" and "ayb". Matching leaf labels do not compensate for the different internal-node labels.
Example 3
labels1 = ["ab"]left1 = [-1]right1 = [-1]labels2 = ["a"]left2 = [-1]right2 = [-1]return = falseThe second text ends after "a", while the first still has "b".
Constraints
- For each tree,
0 <= n <= 5000, and its three arrays have lengthn. - Each label contains only lowercase English letters and has length from
0to1000. - The sum of label lengths in each tree is at most
50000. - Each child index is
-1or a valid node index. The links are acyclic, the root has no parent, and every other node has exactly one parent. - Every listed node is reachable from node
0when the tree is nonempty. Trees may be completely skewed.
More Google Onsite Interview questions
- Route Pattern MatcherPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Evaluate a Nested Math ExpressionPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Distinct Subset SumsONSITE INTERVIEW · Seen Nov 2026
- Playlist With No Same Artist in a RowONSITE INTERVIEW · Seen Nov 2026
- Delete Nodes and Return Forest RootsONSITE INTERVIEW · Seen Nov 2026
- Dice-Walk Winning ProbabilityPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- First and Last Target Position in a Mountain ArrayONSITE INTERVIEW · Seen Nov 2026
- Lowest Common Ancestor in an N-ary TreePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- 0/1 Knapsack with Weight and Volume LimitsONSITE INTERVIEW · Seen Nov 2026
- Batched Subsequence Word MatchesONSITE INTERVIEW · Seen Nov 2026
Similar questions at other companies
- Find File Paths in a Directory TreeUber · PHONE SCREENUber interview questions
- Recover a Tree From Preorder TraversalGrammarly · PHONE SCREEN
- Serialize and Deserialize a Binary TreeNVIDIA · PHONE SCREENNVIDIA interview questions
- JSON Path Lookup with Implicit Array WildcardsMongoDB · ONSITE INTERVIEWMongoDB interview questions
- Parse Nested HTML into a TreeApple · ONSITE INTERVIEWApple interview questions
- Filesystem Hash TreeCursor · PHONE SCREEN · ONSITE INTERVIEWCursor interview questions
- Render a Comment HierarchyAmazon · ONSITE INTERVIEW
- Kth Smallest Element in a Binary Search TreeSalesforce · ONSITE INTERVIEWSalesforce interview questions
- Delete a Filesystem TreeDatadog · PHONE SCREEN · ONSITE INTERVIEWDatadog interview questions
- Flatten and Reconstruct an Ordered TreeThe Walt Disney Company · ONSITE INTERVIEWThe Walt Disney Company interview questions