Smallest String Starting From Leaf

medium binary tree dfs backtracking

Problem

You are given the root of a binary tree where each node has a value in the range 0 to 25 representing the letters 'a' to 'z'. Find the lexicographically smallest string that starts at a leaf of this tree and ends at the root. A leaf is a node with no children. The string is built by reading node letters from the leaf up to the root.

Inputroot = [0,1,2,3,4,3,4]
Output"dba"
Leaf-to-root strings are "dba", "eba", "dca", "eca"; "dba" is the smallest.

def smallest_from_leaf(root):
    best = [None]
    def dfs(node, suffix):
        if not node:
            return
        suffix = chr(ord('a') + node.val) + suffix
        if not node.left and not node.right:
            if best[0] is None or suffix < best[0]:
                best[0] = suffix
            return
        dfs(node.left, suffix)
        dfs(node.right, suffix)
    dfs(root, "")
    return best[0]
function smallestFromLeaf(root) {
  let best = null;
  function dfs(node, suffix) {
    if (!node) return;
    suffix = String.fromCharCode(97 + node.val) + suffix;
    if (!node.left && !node.right) {
      if (best === null || suffix < best) best = suffix;
      return;
    }
    dfs(node.left, suffix);
    dfs(node.right, suffix);
  }
  dfs(root, "");
  return best;
}
class Solution {
    String best = null;
    public String smallestFromLeaf(TreeNode root) {
        dfs(root, "");
        return best;
    }
    private void dfs(TreeNode node, String suffix) {
        if (node == null) return;
        suffix = (char) ('a' + node.val) + suffix;
        if (node.left == null && node.right == null) {
            if (best == null || suffix.compareTo(best) < 0) best = suffix;
            return;
        }
        dfs(node.left, suffix);
        dfs(node.right, suffix);
    }
}
class Solution {
public:
    string best = "";
    bool has = false;
    string smallestFromLeaf(TreeNode* root) {
        dfs(root, "");
        return best;
    }
    void dfs(TreeNode* node, string suffix) {
        if (!node) return;
        suffix = string(1, 'a' + node->val) + suffix;
        if (!node->left && !node->right) {
            if (!has || suffix < best) { best = suffix; has = true; }
            return;
        }
        dfs(node->left, suffix);
        dfs(node->right, suffix);
    }
};
Time: O(n · h) Space: O(h)