Open
Conversation
Tree/README.md
Outdated
| @@ -0,0 +1,286 @@ | |||
| | |||
|
|
|||
| # 树的总结以及 Java 代码模板 | |||
Contributor
There was a problem hiding this comment.
将来还会有其他作者添加其它语言的模板,全文不一定限定在 Java 代码模板。
标题建议用「数据结构」+「算法思想」,例如「树与递归」或者「树与分治」。
Tree/README.md
Outdated
|
|
||
| 树的示例图 | ||
|
|
||
| ### 树的结点: |
Contributor
There was a problem hiding this comment.
以 # 开头的行,后面就不用加 : 了,建议全文检查一下。
Tree/README.md
Outdated
| return res; | ||
| } | ||
|
|
||
| private void traversalHelper(List<Integer> res, TreeNode root) { |
Contributor
There was a problem hiding this comment.
List<Integer> res 这个参数放在第 2 个参数会好一些(建议)。
| ## 学习内容: | ||
|
|
||
| ### 树的遍历(Traversal) : | ||
|
|
Contributor
There was a problem hiding this comment.
可以在介绍遍历的时候,先介绍两种遍历的思想:「深度优先遍历」和「广度优先遍历」(这里可以配图,这篇 题解 里的图可以放在这里)。
「深度优先遍历」每一个结点会访问 3 次(这个提法待讨论,可能会让读者迷惑),在不同的顺序执行不同的操作,就有「前序」「中序」「后序」之分。
「广度优先遍历」以一种类似水波扩散的方式遍历所有结点。
这两种遍历的思想相当重要,需要在这个地方让读者建立起形象的思维。然后可以引入完成这两种遍历需要借助的数据结构「栈」和「队列」。
|
|
||
| 后序遍历的顺序,先遍历左子树,再遍历右子树,最后把根结点加入访问序列。[LeetCode 145](https://leetcode-cn.com/problems/binary-tree-postorder-traversal/) | ||
|
|
||
| 后序遍历的题目非常多,而且难度较高的通常是后序遍历的题,但是只要能够清晰地抓住解题重点,这些题目都能够迎刃而解。做一个形象的比喻,把这个棵树比成一个学校,根结点就像是校长,它的子结点就是各个学院的院长,院长的子结点就是系主任,系主任的子结点是班长,班长的子结点就是班里同学,每个同学就是一个叶结点。如果这时有一个任务,让校长统计学校人数,校长会一个一个人数吗?当然不会,他会让院长去统计每个院的人数,统计完结果之后,自己要做的就是把每个院人数加起来,再加上自己一个人,就是学校总人数了。每个院长,系主任,也都是一样,把任务传递下去,他们做的事情和校长一样,都是统计一下结果,再加上自己。 |
Contributor
There was a problem hiding this comment.
这个例子很好,很形象地说明了后序遍历的应用,在选择例题的时候,可以标注出来哪些是后序遍历思想的应用。
如果可以的话,配一个图可能会让读者印象更深。
Tree/README.md
Outdated
| // 是的话,把他加入统计结果 | ||
| res += root.left.val; | ||
| } | ||
| else { |
Tree/README.md
Outdated
|
|
||
| 前序遍历的顺序,先遍历根结点,再遍历左子树,最后遍历右子树。[LeetCode 102](https://leetcode-cn.com/problems/binary-tree-level-order-traversal/) | ||
|
|
||
| 层序遍历的根据到根结点的距离,逐层从左往右遍历,需要用到 BFS 。关于 BFS 就不在这里进行介绍了。 |
Contributor
There was a problem hiding this comment.
层序遍历和 BFS 是一回事,这里需要重新组织一下语言。层序遍历其实最直观,是可以一句两句说清楚的。
Tree/README.md
Outdated
| if (root == null) { | ||
| return res; | ||
| } | ||
| Queue<TreeNode> q = new LinkedList<>(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
No description provided.