FastPrepInorder Tree String Equivalence
Problems/Inorder Tree String Equivalence

Inorder Tree String Equivalence

Google logoGoogle● MediumINTERNONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Our version is about 95% the same as the reported question. The report leaves out what happens with empty input, which characters are allowed, and the size limits.

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 0 through n - 1. When n > 0, node 0 is the root.
  • labels[i] is the string stored at node i. left[i] and right[i] are its child indices; -1 means 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[]) → boolean

Examples

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 = true

The 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 = false

The 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 = false

The second text ends after "a", while the first still has "b".

Constraints

  • For each tree, 0 <= n <= 5000, and its three arrays have length n.
  • Each label contains only lowercase English letters and has length from 0 to 1000.
  • The sum of label lengths in each tree is at most 50000.
  • Each child index is -1 or 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 0 when 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

Google interview experiences

  • Google interviews · November 2026
  • Google Software Engineer Onsite Questions: 10 Experiences
  • Google OA: What It Tests (2026)
  • Google L4 SWE Phone Screen: 1 Experience
  • Google ML Engineering Manager Onsite: 1 Experience
  • Google Experienced SWE First Round and Onsite: 1 Experience
All Google interview questionsSee Google hiring insights
Saved
class Solution {
    public boolean equivalentInorderText(String[] labels1, int[] left1, int[] right1, String[] labels2, int[] left2, int[] right2) {
        // write your code here
    }
}
Checking account…