Compare N-Ary Tree Leaf Sequences
Updated
Problem statement
Each N-ary tree is represented by parallel parent and values arrays. The root has parent -1; every other entry names its parent index. Children are ordered by node index. Return whether the two trees have identical leaf-value sequences from left to right. Use an iterative traversal.
Function
sameNaryLeafValues(parent1: int[], values1: int[], parent2: int[], values2: int[]) → booleanExamples
Example 1
parent1 = [-1,0,0]values1 = [1,2,3]parent2 = [-1,0,0]values2 = [7,2,3]return = trueBoth leaf sequences are [2,3].
Example 2
parent1 = [-1,0,0]values1 = [1,2,3]parent2 = [-1,0,0]values2 = [1,3,2]return = falseLeaf order differs.
Constraints
- Each tree has between
1and200000nodes. - Each input is a valid rooted tree with exactly one
-1parent. - Node values fit in a signed 32-bit integer.
More Navan interview questions
- Time Based Key-Value StoreONSITE INTERVIEW · Seen Dec 2025
- Type-Ahead Prefix MatchesONSITE INTERVIEW · Seen Dec 2024
- Minimum Steps on a Circular Character DialONSITE INTERVIEW · Seen Jan 2024
- Minimum Stick Connection CostONSITE INTERVIEW · Seen Jan 2024
- Shortest Substring with at Least K Distinct CharactersONSITE INTERVIEW · Seen Jan 2024
- Roman to IntegerPHONE SCREEN · Seen Oct 2024
- Sort ColorsPHONE SCREEN · Seen Oct 2024
- Basic CalculatorPHONE SCREEN · Seen Jun 2024
- LRU CachePHONE SCREEN · Seen Jun 2024
- House RobberPHONE SCREEN · Seen May 2024
Similar questions at other companies
- Filesystem Entity Total SizeGoogle · ONSITE INTERVIEWGoogle 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
- Inorder, Preorder and Postorder TraversalsAMD · ONSITE INTERVIEWAMD interview questions
- Flatten JSON into Repeated Key-Value PairsKlaviyo · ONSITE INTERVIEWKlaviyo interview questions
- Kth Smallest Value in a BSTExpedia · PHONE SCREEN · ONSITE INTERVIEWExpedia interview questions
- Validate Binary Search TreeInMobi · ONSITE INTERVIEWInMobi interview questions
- Serialize a Binary Search TreeConfluent · ONSITE INTERVIEWConfluent interview questions