Skip to content

Commit

Permalink
fix: 增加图
Browse files Browse the repository at this point in the history
  • Loading branch information
lucifer committed Jan 22, 2020
1 parent b8e8fa5 commit b0b69f8
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 5 deletions.
3 changes: 0 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -180,9 +180,6 @@ leetcode 题解,记录自己的 leetcode 解题之路。
- [0094.binary-tree-inorder-traversal](./problems/94.binary-tree-inorder-traversal.md)
- [0095.unique-binary-search-trees-ii](./problems/95.unique-binary-search-trees-ii.md) 🆕
- [0096.unique-binary-search-trees](./problems/96.unique-binary-search-trees.md) 🆕

95.unique-binary-search-trees-ii

- [0098.validate-binary-search-tree](./problems/98.validate-binary-search-tree.md)
- [0102.binary-tree-level-order-traversal](./problems/102.binary-tree-level-order-traversal.md)
- [0103.binary-tree-zigzag-level-order-traversal](./problems/103.binary-tree-zigzag-level-order-traversal.md)
Expand Down
2 changes: 2 additions & 0 deletions problems/211.add-and-search-word-data-structure-design.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ search("b..") -> true

关于前缀树,LeetCode 有很多题目。有的是直接考察,让你实现一个前缀树,有的是间接考察,比如本题。前缀树代码见下方,大家之后可以直接当成前缀树的解题模板使用。

![](https://tva1.sinaimg.cn/large/006tNbRwly1gb5dmstsxxj30mz0gqmzh.jpg)

由于我们这道题需要考虑特殊字符".",因此我们需要对标准前缀树做一点改造,insert 不做改变,我们只需要改变 search 即可,代码(Python 3):

```python
Expand Down
6 changes: 4 additions & 2 deletions problems/212.word-search-ii.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,17 @@ words = ["oath","pea","eat","rain"] and board =

我们需要对矩阵中每一项都进行深度优先遍历(DFS)。 递归的终点是

- 1. 超出边界
- 2. 递归路径上组成的单词不在 words 的前缀。
1. 超出边界
2. 递归路径上组成的单词不在 words 的前缀。

比如题目示例:words = ["oath","pea","eat","rain"],那么对于 oa,oat 满足条件,因为他们都是 oath 的前缀,但是 oaa 就不满足条件。

为了防止环的出现,我们需要记录访问过的节点。而返回结果是需要去重的。出于简单考虑,我们使用集合(set),最后返回的时候重新转化为 list。

刚才我提到了一个关键词“前缀”,我们考虑使用前缀树来优化。使得复杂度降低为$O(h)$, 其中 h 是前缀树深度,也就是最长的字符串长度。

![](https://tva1.sinaimg.cn/large/006tNbRwly1gb5dmstsxxj30mz0gqmzh.jpg)

## 关键点

- 前缀树(也叫字典树),英文名 Trie(读作 tree 或者 try)
Expand Down

0 comments on commit b0b69f8

Please sign in to comment.