Skip to main content

Command Palette

Search for a command to run...

Solving Leetcode Interviews in Seconds with AI: Smallest String Starting From Leaf

Updated
2 min read

Introduction

In this blog post, we will explore how to solve the LeetCode problem "988" using AI. LeetCode is a popular platform for preparing for coding interviews, and with the help of AI tools like Chatmagic, we can generate solutions quickly and efficiently - helping you pass the interviews and get the job offer without having to study for months.

Problem Statement

You are given the root of a binary tree where each node has a value in the range [0, 25] representing the letters 'a' to 'z'. Return 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: root = [0,1,2,3,4,3,4] Output: "dba" Example 2: Input: root = [25,1,3,1,3,0,2] Output: "adz" Example 3: Input: root = [2,2,1,null,1,0,null,0] Output: "abc" Constraints: The number of nodes in the tree is in the range [1, 8500]. 0 <= Node.val <= 25

Explanation

Here's a solution to find the lexicographically smallest string from leaf to root in a binary tree:

  • Depth-First Search (DFS): Traverse the tree using DFS, exploring each path from leaf to root.
  • String Construction: As we traverse, build the string from leaf to root.
  • Lexicographical Comparison: Compare the strings formed from different paths and keep track of the smallest one encountered so far.

  • Runtime Complexity: O(N), where N is the number of nodes in the tree.

  • Storage Complexity: O(H), where H is the height of the tree (due to recursion stack). In the worst case (skewed tree), H can be N, and in the best case (balanced tree), H can be log N.

Code

    class TreeNode:
    def __init__(self, val=0, left=None, right=None):
        self.val = val
        self.left = left
        self.right = right

def smallestFromLeaf(root: TreeNode) -> str:
    """
    Finds the lexicographically smallest string from leaf to root in a binary tree.
    """

    def dfs(node: TreeNode, current_string: str) -> str:
        """
        Performs depth-first search to find the smallest string.
        """
        if not node:
            return ""

        char = chr(ord('a') + node.val)
        new_string = char + current_string  # Prepend the character

        if not node.left and not node.right:  # Leaf node
            return new_string

        left_string = dfs(node.left, new_string) if node.left else None
        right_string = dfs(node.right, new_string) if node.right else None

        if left_string is None:
            return right_string or ""
        if right_string is None:
            return left_string

        return min(left_string, right_string)

    return dfs(root, "")

More from this blog

C

Chatmagic blog

2894 posts