Build strings from each leaf to the root and return the lexicographically smallest one.
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 , , 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 .
- 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
- number of nodes
- Node values are integers in
- The tree contains at least one leaf
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.