FastPrepInorder Tree String Equivalence

Inorder Tree String Equivalence

Google logoGoogle● MediumINTERNONSITE INTERVIEW

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 concatenating 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:

The problem statement continues
Pro

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".

FastPrep Pro
Reported in 1 Google interview this week

Unlock this recently reported problem

FastPrep Pro gives you full access to interview problems reported within the last week.

  • Full problem statement and constraints
  • 2 more worked examples, explained
  • Guided hints and editorial
  • Run your code on real test cases
$8.25/month

$99 billed yearly — or $19 month-to-month. Cancel anytime.

Free plan — 2 of 2 free unlocks used this week
See Google hiring insights
CodePython 3
Run and Submit unlock with Pro
FastPrep Pro
Reported in 1 Google interview this week

Unlock this recently reported problem

FastPrep Pro gives you full access to interview problems reported within the last week.

  • Full problem statement and constraints
  • 2 more worked examples, explained
  • Guided hints and editorial
  • Run your code on real test cases
$8.25/month

$99 billed yearly — or $19 month-to-month. Cancel anytime.

Free plan — 2 of 2 free unlocks used this week