FunctionRacerProblemsRace

Smallest String Starting From Leaf

MediumTreesPython, JavaScript, TypeScript, Java, C++

You are given the root of a binary tree. Every node holds a number 0 to 25, standing for the letters a to z (0 is a, 25 is z). Each path from a leaf up to the root spells a word (the leaf's letter first, the root's letter last). Return the alphabetically smallest of these words. A shorter word that is the beginning of a longer one counts as smaller. The tree has at least one node.

Trees appear in the tests in level order, with null for a missing child, like [3,9,20,null,null,15,7]. Each node is a TreeNode with .val, .left and .right; the class is already defined for you.

Example 1

root [0,1,2,3,4,3,4]
returns "dba"

Leaf words: "dba", "eba", "dca", "eca". The smallest is "dba".

Constraints

Race itor take your time

Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.

More Trees problems