logo
Algorithms study cheatsheets

Coding interview 的 Trie cheatsheet

Trie 学习指南,包含练习题、技巧、时间复杂度与推荐资源

Introduction

Trie 是一种特殊的 tree(prefix tree),让字符串的存储与检索更高效。Trie 在搜索和 autocomplete 等场景非常常见。熟悉这些应用场景,能让你在面试中更快判断哪些题目适合用 trie 解决。

要能从零实现一个 Trie class,包括 addremovesearch 方法。

Learning resources

Time complexity

m 是操作时的字符串长度。

OperationBig-O
SearchO(m)
InsertO(m)
RemoveO(m)

Corner cases

  • Searching for a string in an empty trie
  • Inserting empty strings into a trie

Techniques

把一组单词(list)预处理成 trie,能让在 n 个单词里搜索长度为 k 的单词从 O(n) 降到 O(k)。

Essential questions

这些是该 topic 必练题。

这些是在掌握基础后推荐练习的题目。

检测你的掌握度

3
1/3

Q1. Trie 中搜索一个长度为 m 的单词的时间复杂度是?

相关练习题

Coding interview 的 Trie cheatsheet

暂无相关练习题