Skip to main content
Back to problems
Leetcode
Medium
Trees
Strings
Recursion
Smallest String Starting From Leaf

Build strings from each leaf to the root and return the lexicographically smallest one.

Acceptance 0%
Problem Statement

Given the root of a binary tree where each node contains a value from 0 to 25, treat the value as a lowercase letter with 0a0 \to a, 1b1 \to b, and so on.

For every leaf node, form a string by reading letters from that leaf up to the root. Among all such strings, return the one that is lexicographically smallest.

A leaf is a node with no children.

Input Format

  • A binary tree root.
  • Each node stores an integer in the range [0,25][0, 25].
  • The tree is provided in the platform's standard tree format.

Output Format

  • Return a single string: the lexicographically smallest string formed from any leaf to the root.

Constraints

  • 11 \le number of nodes 104\le 10^4
  • Node values are integers in [0,25][0, 25]
  • The tree contains at least one leaf
Examples
Sample cases returned by the problem API.

Example 1

Input

root = [0,1,2,3,4,3,4]

Output

dba

Explanation

The leaf-to-root strings are "dba", "eba", "dca", and "eca". The smallest lexicographically is "dba".

Example 2

Input

root = [25,1,3,1,3,0,2]

Output

adz

Explanation

The leaf-to-root strings include "adz", "bzz", and others. The smallest lexicographically is "adz".

Premium problem context

Unlock deeper context for this problem

Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.

Guided hints
Editorial and discussion links
Concept map and variants
Sign in to unlock
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.