Similar Problems

Similar Problems not available

Smallest String Starting From Leaf - Leetcode Solution

Companies:

LeetCode:  Smallest String Starting From Leaf Leetcode Solution

Difficulty: Medium

Topics: string tree binary-tree depth-first-search  

Problem Statement: Given the root of a binary tree, each node has a value from 0 to 25 representing the letters 'a' to 'z': a value of 0 represents 'a', a value of 1 represents 'b', and so on.

Find the lexicographically smallest string that starts at a leaf of this tree and ends at the root.

As a reminder, any shorter prefix of a string is lexicographically smaller: for example, "ab" is lexicographically smaller than "aba". A leaf of a node is a node that has no children.

Example 1: Input: [0,1,2,3,4,3,4] Output: "dba"

Solution: To solve this problem, we need to perform a recursive traversal of the binary tree starting from the root and traversing down to each leaf. For each leaf node, we need to construct a string starting from that leaf all the way up to the root, and return the lexicographically smallest such string. To obtain the lexicographically smallest string, we can compare the current string constructed so far with the candidate string obtained from the left and right subtrees.

The general approach is as follows:

  1. Start at the root of the binary tree, and recursively traverse the tree from top to bottom.
  2. At each node of the tree, we need to construct a string starting from that node all the way down to the leaf nodes.
  3. If the current node has no left or right child, we have reached a leaf node, and we can return a string containing only the letter at that node.
  4. If the current node has both left and right children, we need to find the lexicographically smallest string that starts at one of the leaf nodes of the left subtree, and the lexicographically smallest string that starts at one of the leaf nodes of the right subtree.
  5. We can compare these two candidate strings and return the lexicographically smallest one.
  6. Finally, we append the letter at the current node to the string obtained in step 5, and return this string to the parent node.

Here is the Python code to implement the solution:

Definition for a binary tree node.

class TreeNode:

def init(self, x):

self.val = x

self.left = None

self.right = None

class Solution: def smallestFromLeaf(self, root: TreeNode) -> str:

    def construct_string(node: TreeNode) -> str:
        if node is None:
            return ""
        if node.left is None and node.right is None:
            return chr(node.val + ord('a'))
        left_str = construct_string(node.left)
        right_str = construct_string(node.right)
        if left_str != "" and right_str != "":
            return min(left_str, right_str) + chr(node.val + ord('a'))
        elif left_str != "":
            return left_str + chr(node.val + ord('a'))
        else:
            return right_str + chr(node.val + ord('a'))
        
    return construct_string(root)

Time Complexity: The time complexity of the above solution is O(NlogN), where N is the number of nodes in the binary tree.

Space Complexity: The space complexity of the above solution is O(H), where H is the height of the binary tree.

Smallest String Starting From Leaf Solution Code

1