Smallest String Starting From Leaf - Problem

Imagine you're working with a special binary tree where each node contains a letter from 'a' to 'z' (represented by values 0-25). Your task is to find the lexicographically smallest string that starts from any leaf node and travels up to the root.

Think of it like finding the "earliest" path in a dictionary - just like how "ab" comes before "aba" alphabetically. You need to explore all possible paths from leaves to root and return the one that would appear first in a dictionary.

Key Points:

  • A leaf node has no children
  • The string is built from leaf → root direction
  • Each node value (0-25) maps to letters 'a'-'z'
  • Return the lexicographically smallest such string

Example: If a path gives you nodes [7, 4, 11, 11, 14] from leaf to root, this translates to string "hello".

Input & Output

example_1.py — Basic Tree
$ Input: root = [0,1,2,3,4,3,4] Tree structure: a(0) / \ b(1) c(2) / | | \ d(3) e(4) d(3) e(4)
Output: "dba"
💡 Note: There are four possible paths from leaves to root: "dba", "eba", "dca", "eca". The lexicographically smallest is "dba".
example_2.py — Single Path
$ Input: root = [25,1,3,1,3,0,2] Tree structure: z(25) / \ b(1) d(3) / | | \ b(1) d(3) a(0) c(2)
Output: "adz"
💡 Note: The paths are "bbz", "ddz", "adz", "cdz". The smallest lexicographically is "adz".
example_3.py — Edge Case - Single Node
$ Input: root = [0] Tree structure: a(0)
Output: "a"
💡 Note: Single node tree, so the only path from leaf to root is just "a".

Constraints

  • The number of nodes in the tree is in the range [1, 8500]
  • 0 ≤ Node.val ≤ 25
  • Each node value represents a letter from 'a' to 'z' (0 = 'a', 1 = 'b', ..., 25 = 'z')
  • Important: The string is built from leaf to root, not root to leaf

Visualization

Tap to expand
Smallest String Starting From Leaf INPUT Binary Tree Structure: a b c d e d e Input Array: [0, 1, 2, 3, 4, 3, 4] Values 0-25 map to 'a'-'z' Leaves: d, e, d, e ALGORITHM (DFS) 1 DFS Traversal Traverse tree, build path string from root to leaf 2 Build Leaf-to-Root String At each leaf, reverse path to get leaf-to-root string 3 Compare Strings Compare lexicographically Keep smallest result 4 Return Smallest Return lexicographically smallest string found All Leaf-to-Root Paths: d --> b --> a = "dba" e --> b --> a = "eba" d --> c --> a = "dca" e --> c --> a = "eca" SMALLEST FINAL RESULT Winning Path Highlighted: a b c d e d e Path: d --> b --> a Output: "dba" OK - Lexicographically Smallest String Found Key Insight: DFS builds strings from root to leaf, but we need leaf-to-root strings for comparison. At each leaf node, reverse the accumulated path string and compare lexicographically. "dba" < "dca" < "eba" < "eca" because 'b' < 'c' and 'd' < 'e' in ASCII ordering. TutorialsPoint - Smallest String Starting From Leaf | DFS Approach
Asked in
Google 42 Amazon 38 Microsoft 25 Meta 18
42.8K Views
Medium Frequency
~18 min Avg. Time
1.2K 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