Solution Review: Total Number of Words in a Trie
Learn a detailed analysis of the different ways to solve the “Total Number of Words in a Trie” challenge.
We'll cover the following...
Solution: Increment recursively
Press + to interact
main.cs
Trie.cs
using System;namespace chapter_7{class Program{static int totalWords(TrieNode root){int result = 0;// Leaf denotes end of a wordif (root.isEndWord)result++;for (int i = 0; i < 26; i++)if (root.children[i] != null)result += totalWords(root.children[i]);return result;}static void Main(string[] args){string [] keys = { "the", "a", "there", "answer", "any", "by", "bye", "their", "abc" };Trie t = new Trie();// Construct triefor (int i = 0; i < 9; i++){t.insertNode(keys[i]);}Console.WriteLine( totalWords(t.getRoot()));return;}}}
This is a straightforward algorithm. Starting ...