Path Between Two Binary-Tree Nodes
Updated
Problem statement
You are given a binary tree with n nodes, labeled 0 through n - 1. The array children describes the tree:
children[v] = [leftChild, rightChild]gives the child IDs of nodev.-1means there is no child on that side.
The root can have any node ID. IDs are just labels; they do not tell you the order of nodes in the tree.
Find the path from node start to node finish. You can move from a parent to a child or from a child to its parent. The path must not contain the same node more than once. There is exactly one such path.
Return the node IDs along this path, in order from start to finish, including both. If start and finish are the same, return [start]. Do not change children.
Function
treePath(children: int[][], start: int, finish: int) → int[]Examples
Example 1
children = [[1,2],[3,4],[5,6],[-1,-1],[-1,-1],[-1,-1],[-1,-1]]start = 3finish = 6return = [3,1,0,2,6]The path moves from node 3 up through 1 and 0, then down through 2 to 6.
Example 2
children = [[-1,-1],[-1,-1],[3,6],[1,4],[-1,-1],[0,2],[-1,-1]]start = 0finish = 4return = [0,5,2,3,4]The root is node 5. Node labels do not give parent order, and moving upward is allowed.
Example 3
children = [[-1,-1]]start = 0finish = 0return = [0]A path from a node to itself contains that node exactly once.
Constraints
1 <= n <= 2000; every row has exactly two integers.- Every child is
-1or a node ID in0..n-1. - The input is one valid connected binary tree: one root has no parent, every other node has exactly one parent, and there are no cycles.
0 <= start, finish < n.
More Snowflake Phone Screen questions
- Connect Four from the Last MovePHONE SCREEN · Seen Nov 2026
- Minimum Edits to a Perfect Binary TreePHONE SCREEN · Seen Nov 2026
- Configurable Per-Client Token BucketsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Find Valid SET Card TriplesOA · PHONE SCREEN · Seen Nov 2026
- Minimum Index Distance Between Person and CakeOA · PHONE SCREEN · Seen Nov 2026
- Minimum N-ary Tree Depth DeletionsOA · PHONE SCREEN · Seen Nov 2026
- Sum of Binary Tree Boundary NodesOA · PHONE SCREEN · Seen Nov 2026
- Greedy Person-Cake Matching DistancePHONE SCREEN · Seen Nov 2026
- Tree Height After Child PromotionPHONE SCREEN · Seen Oct 2026
- Minimum StackPHONE SCREEN · Seen Oct 2026
Similar questions at other companies
- Insert an Employee into an Organization HierarchyWaymo · PHONE SCREENWaymo interview questions
- Bottom View of a Binary TreeOracle · PHONE SCREEN · ONSITE INTERVIEWOracle interview questions
- Decrypt a String from Binary Tree ColumnsBloomberg LP · PHONE SCREENBloomberg LP interview questions
- Transform and Prune a Mode-Valued Binary TreeGoogle · PHONE SCREENGoogle interview questions
- Everyone Under a ManagerAdobe · PHONE SCREENAdobe interview questions
- Team Cost Under a ManagerRippling · PHONE SCREENRippling interview questions
- Count LevelsBlackRock · OABlackRock interview questions
- Vertical Order Traversal of a Binary TreeAmazon · ONSITE INTERVIEW
- Binary Tree Vertical Order with Stable TiesMeta · ONSITE INTERVIEWMeta interview questions
- Top View of a Binary TreeInMobi · ONSITE INTERVIEWInMobi interview questions