Inorder Tree String Equivalence
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:
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 = 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".
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
$99 billed yearly — or $19 month-to-month. Cancel anytime.