0% completed
Problem Statement
You are given a binary tree where each node contains a value between 0
and 25
. These values correspond to the letters 'a' to 'z' (0 = 'a', 1 = 'b', ..., 25 = 'z').
Return the lexicographically smallest string that can be formed by starting from a leaf node (a node with no children) and ending at the root of the tree. The string formed should be the lexicographically smallest among all possible strings.
Remember, any shorter prefix of a string is lexicographically smaller. For example, in lexicographical order, "ab" is smaller than "aba".
Examples
Example 1:
- Input: root =
[0, 1, 2, null, 3, 3, 4]
- Expected Output:
"dba"
- Explanation: The strings formed from the leaf nodes to the root are "dba", "dca", and "eca". The smallest string is "dba".
Example 2:
- Input: root =
[4, 0, 1, 1, 3, 2, 3]
- Expected Output:
"bae"
- Explanation: The strings formed are "bae", "dae", "cbe", and "dbe". The smallest string is "bae".
Example 3:
- Input: root =
[1, 3, 2, 3, 4, 4, 3]
- Expected Output:
"dcb"
- Explanation: The strings formed are "ddb", "edb", "ecb", and "dcb". The smallest string is "dcb".
Constraints:
- The number of nodes in the tree is in the range
[1, 8500]
. 0 <= Node.val <= 25
Solution
To solve this problem, we employ a Depth First Search (DFS) approach to traverse the binary tree from the root to all leaf nodes. As we visit each node, we construct strings by converting the node's value to its corresponding character and appending it to a suffix string. When a leaf node is reached, the constructed string, which represents the path from that leaf to the root, is returned.
The algorithm then compares these strings from different paths, ensuring that the smallest lexicographically string is selected. By using DFS, we efficiently explore all possible paths, and by maintaining the smallest string found so far, we ensure that the final result is the smallest possible string starting from a leaf and moving towards the root.
Step-by-Step Algorithm
-
Start at the Root:
- Start the DFS from the root node.
-
Check for Null Node:
- Inside the
dfs
method, the first action is to check if the current node (node
) isnull
. - If the node is
null
, return a string with the value"~"
. This serves as a sentinel value since it is lexicographically larger than any valid string formed by the characters'a'
to'z'
.
- Inside the
-
Update the Suffix String:
- Convert the value of the current node to its corresponding character by adding the node’s value to the ASCII value of
'a'
. - Prepend this character to the
suffix
string as we need to createleaf to root
string.
- Convert the value of the current node to its corresponding character by adding the node’s value to the ASCII value of
-
Check for Leaf Node:
- If the current node is a leaf, return the current
suffix
string as it represents a valid path from a leaf to the root.
- If the current node is a leaf, return the current
-
Recursive DFS on Child Nodes:
- Recursively call the
dfs
method on both the left and right children of the current node. - Pass the updated
suffix
string in these recursive calls.
- Recursively call the
-
Compare and Select the Smaller String:
- Compare the strings returned by the left and right recursive calls using the
minimum
method. - The
minimum
method returns the lexicographically smaller string.
- Compare the strings returned by the left and right recursive calls using the
-
Return the Smallest String:
- Return the smallest string obtained from the comparisons. This string is propagated up the recursive call stack, eventually reaching the original call in the
smallestFromLeaf
method.
- Return the smallest string obtained from the comparisons. This string is propagated up the recursive call stack, eventually reaching the original call in the
Algorithm Walkthrough
Given the binary tree structure for the input:
0
/ \
1 2
\ / \
3 3 4
-
Start at Root:
- The
smallestFromLeaf
method is called with the root node (0
) and an empty string""
.
- The
-
DFS on Root Node:
- The
dfs
method starts with node0
. The currentsuffix
is"a"
because0 + 'a' = 'a'
. - The node
0
is not a leaf, so proceed to its children.
- The
-
DFS on Left Subtree (Node
1
):- The
dfs
method is called recursively with node1
and the currentsuffix
"a"
. - The current
suffix
becomes"ba"
because1 + 'a' = 'b'
. - Node
1
is not a leaf, so move to its children.
- The
-
DFS on Node
3
(Right Child of Node1
):- The
dfs
method is called recursively with node3
and the currentsuffix
"ba"
. - The current
suffix
becomes"dba"
because3 + 'a' = 'd'
. - Node
3
is a leaf, so return"dba"
.
- The
-
DFS on Right Subtree (Node
2
):- After completing the left subtree, the algorithm moves to the right subtree by calling
dfs
on node2
with the initialsuffix
"a"
. - The current
suffix
becomes"ca"
because2 + 'a' = 'c'
. - Node
2
is not a leaf, so move to its children.
- After completing the left subtree, the algorithm moves to the right subtree by calling
-
DFS on Node
3
(Left Child of Node2
):- The
dfs
method is called recursively with node3
and the currentsuffix
"ca"
. - The current
suffix
becomes"dca"
because3 + 'a' = 'd'
. - Node
3
is a leaf, so return"dca"
.
- The
-
DFS on Node
4
(Right Child of Node2
):- The
dfs
method is called recursively with node4
and the currentsuffix
"ca"
. - The current
suffix
becomes"eca"
because4 + 'a' = 'e'
. - Node
4
is a leaf, so return"eca"
.
- The
-
Compare Strings from Right Subtree:
- The algorithm now has two strings from the right subtree:
"dca"
and"eca"
. - Compare these strings using the
minimum
method."dca"
is smaller, so"dca"
is returned up the call stack.
- The algorithm now has two strings from the right subtree:
-
Compare Left and Right Subtree Results:
- The algorithm compares
"dba"
(from the left subtree) and"dca"
(from the right subtree) at the root node. "dba"
is smaller, so"dba"
is returned as the final result.
- The algorithm compares
-
Output the Result:
- The smallest string
"dba"
is printed as the output.
- The smallest string
Code
Complexity Analysis
Time Complexity
- The time complexity of the algorithm is O(N), where
N
is the number of nodes in the binary tree. - This is because the algorithm performs a Depth First Search (DFS), which visits each node exactly once.
Space Complexity
- The space complexity is O(H), where
H
is the height of the tree. - This space is required for the recursive stack used during DFS. In the worst case, if the tree is skewed, the height
H
could be equal toN
, leading to a space complexity of O(N). - In the best case, the tree is balanced, and the space complexity would be O(log N).
.....
.....
.....
On this page
Problem Statement
Examples
Solution
Step-by-Step Algorithm
Algorithm Walkthrough
Code
Complexity Analysis
Time Complexity
Space Complexity