Check If a String Is a Valid Sequence from Root to Leaves Path in a Binary Tree - Problem
Path Detective: Finding Valid Sequences in Binary Trees

Imagine you're a detective following clues through a maze-like tree structure. You have a binary tree where each node contains a single digit, and you need to determine if a given sequence of numbers forms a valid path from root to leaf.

Given:
β€’ A root node of a binary tree where each node contains an integer value
β€’ An array arr of integers representing a sequence

Your mission: Return true if there exists a path from the root to any leaf node such that the values along the path exactly match the given array sequence. The path must:
1. Start at the root node
2. End at a leaf node (node with no children)
3. Follow parent-child relationships in the tree
4. Match the array values in exact order

Think of it like following a treasure map - you must follow the exact sequence of landmarks to reach the treasure chest at the end!

Input & Output

example_1.py β€” Valid Path Found
$ Input: root = [1,0,1,null,1,0,null,null,1,0,null], arr = [1,0,1]
β€Ί Output: true
πŸ’‘ Note: The path 1 β†’ 0 β†’ 1 exists from root to leaf and matches the target sequence [1,0,1] exactly.
example_2.py β€” No Valid Path
$ Input: root = [1,0,1,null,1,0,null,null,1,0,null], arr = [1,1,1]
β€Ί Output: false
πŸ’‘ Note: No path from root to leaf matches [1,1,1]. The root is 1, but its children are 0 and 1, so we can't have 1β†’1 as the first two elements.
example_3.py β€” Path Doesn't End at Leaf
$ Input: root = [1,0,1,null,1,0,null,null,1,0,null], arr = [1,0]
β€Ί Output: false
πŸ’‘ Note: The sequence [1,0] exists as a path from root, but it doesn't end at a leaf node. Valid sequences must reach a leaf.

Visualization

Tap to expand
πŸšͺSTART (1)πŸ›οΈLandmark (0)🏭Wrong Path (1)πŸ’ŽTREASURE! (1)🚧Dead End (6)πŸ—ΊοΈ Treasure Map: [1, 0, 1]βœ… Path: πŸšͺ(1) β†’ πŸ›οΈ(0) β†’ πŸ’Ž(1) = SUCCESS!❌ Path: πŸšͺ(1) β†’ 🏭(1) = ABANDONED (wrong landmark)🧠 Smart exploration saves time by abandoning wrong paths early
Understanding the Visualization
1
Start Journey
Begin at the entrance (root) and check if it matches your first landmark
2
Follow Map
At each step, verify the current location matches your expected landmark
3
Smart Abandonment
If you reach a wrong landmark, abandon that path immediately - no need to continue
4
Treasure Check
Only claim success if you reach a dead-end AND have followed the complete map
Key Takeaway
🎯 Key Insight: By checking each node against our target sequence during traversal, we can eliminate invalid paths early and find valid sequences efficiently in O(n) time with minimal space usage.

Time & Space Complexity

Time Complexity
⏱️
O(nΒ²)

O(n) to traverse all nodes, and potentially O(n) space per path with up to n paths in worst case

n
2n
⚠ Quadratic Growth
Space Complexity
O(nΒ²)

In worst case (skewed tree), we might store O(n) paths each of length O(n)

n
2n
⚠ Quadratic Space

Constraints

  • The number of nodes in the tree is in the range [1, 5000]
  • 1 ≀ Node.val ≀ 9
  • 1 ≀ arr.length ≀ 50
  • 0 ≀ arr[i] ≀ 9
  • All Node.val are digits
  • The tree is guaranteed to be a valid binary tree
Asked in
Facebook 45 Amazon 32 Google 28 Microsoft 19
42.4K Views
Medium Frequency
~18 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
πŸ’‘ Explanation
AI Ready
πŸ’‘ Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen