# Solution: Find All Words Stored in Trie

This review provides a detailed analysis of the different ways to solve the Find All Words Stored in Trie Challenge.

## We'll cover the following

## Statement

Given a trie data structure representing a list of words, implement a function that finds and returns all words stored in the trie.

**Constraints:**

$0\leq$ `words.length`

$\leq 10^3$ $1\leq$ `words[i].length`

$\leq10^2$ All

`words[i]`

consist of lowercase English letters.

## Solution

This solution utilizes a trie data structure, efficiently organizing a list of words. In a trie, each word is broken down into letters, each represented by a node in the trie. Starting from the root node, each subsequent letter in the word corresponds to a child node of the previous letter’s node. This process continues until the entire word is represented by a path from the root node to a node marked as the end of the word. The algorithm explores each node by recursively traversing the trie from the root node, effectively enumerating all possible character combinations that form valid words. At each node, it checks if it marks the end of a word, constructing the word by accumulating characters from the word array. This recursive exploration continues until all words in the trie are found and stored in the result list. Let’s look at how each function behaves and contributes to this approach:

The

`getMaxDepth`

function recursively determines the maximum depth of a tree. It starts from a given root node and traverses through the tree’s children, incrementing the level by one each time. This recursive traversal continues until it reaches the leaf nodes. At each level, the function compares the current depth with the maximum depth encountered so far, updating the maximum depth if necessary.The

`findWords`

function recursively retrieves words from a tree structure. It initializes an empty list to store words and a list with a size determined by the tree’s maximum depth. As it traverses the tree, it retrieves the characters at each level to form words, appending them to the result list when a word is complete. Finally, it returns the list of extracted words.The

`getWords`

function utilizes a**depth-first search (DFS)**approach to traverse a trie data structure. It starts from the given root node and recursively explores each branch. At each node, the algorithm checks if it marks the end of a word. If it does, it constructs the word by concatenating characters stored in the word array to the current level and adds it to the result list. Then, the algorithm iterates over the current node’s children, representing possible next characters in the words. For each child, it updates the word array with the character at the current level and recursively explores the child node. This process continues until all possible paths in the trie are explored, generating all words stored in the trie.

Let’s look at the illustration below to better understand the solution:

Create a free account to view this lesson.

By signing up, you agree to Educative's Terms of Service and Privacy Policy