FastPrepLexicographically Smallest Root-to-Leaf String

Lexicographically Smallest Root-to-Leaf String

Bloomberg LP logoBloomberg LP● MediumNEW GRADONSITE INTERVIEW
Learn

Problem statement

Each binary-tree node contains an integer from 0 through 25 representing letters a through z. Return the lexicographically smallest string encountered along a path from the root to any leaf.

Return the empty string for an empty tree.

Function

smallestRootToLeaf(root: TreeNode) → String

Examples

Example 1

root = [0,1,2,3,4,3,4]return = "abd"

The root-to-leaf strings are abd, abe, acd, and ace; abd is smallest.

Constraints

  • The tree contains at most 10^5 nodes.
  • Every node value is between 0 and 25.

More Bloomberg LP problems

See Bloomberg LP hiring insights
public String smallestRootToLeaf(TreeNode root) {
  // Write your code here.
}
root[0,1,2,3,4,3,4]
expected"abd"
Checking account…