首先最最基本的就是前序后序中序层次遍历。这个要闭着眼睛都能写出来其递归形式和遍历形式。

递归形式是我们非常熟悉的 利用helper辅助函数,进行递归 递归函数非常简单 只需要注意每次add放置的位置即可。而且要注意对于root是否为null的判断。唯一需要强调的是层次遍历,我们的递归函数有三个变量,第一个是二维数组,第二个是输入的root, 第三个是level 我们采取夹心涂层的方式进行写入(为什么要采用夹心涂层方法遍历?因为level order是BFS)。

而遍历形式则比较复杂 前序中序后序使用一个stack来进行实现的(因为这三个是基于DFS),而层次遍历是采用queue来实现的(是基于BFS).有一说一 里面的细节是真鸡儿的多!所以还是要靠理解,绝对不能死记硬背。

注意,前序和后序遍历的遍历形式代码比较像,中序会跟前两个有些略微不同,有两个while,至于为什么有两个 你要想到中序是每次必要深入到最左节点就行了。

然后就是一些进阶的问题了

这些进阶的问题中 有些给定

首先是不要求对树做任何改变:

以各种奇奇怪怪的方式输出这个树(reverse level order, zigzag level order)

判断两个树是否相等

判断两个树是否对称

判断给定的树是否平衡

判断给定的树最大深度/最小深度

判断给定二叉树是否是BST

然后就是千奇百怪的对树做处理:

这种问题实在太多 无法一一列举

and then there are third general kind of tree: construct a tree

  • 关于尾递归和helper()递归的更正

为什么有的时候可以用尾递归 有的时候一定要用helper()?因为尾递归是递归的当前函数 此函数的参数都是给死的。但是用helper()之后可以对递归函数做任何改造,所以我觉得现在直接叫尾递归和helper()递归不合适 应该叫拓展递归(用helper函数)和非拓展递归(题目给定的函数可以直接递归出答案) 至于递归函数使用尾递归还是双路递归还是其他什么奇奇怪怪的递归 都不重要了.

  • there are two kinds of recursion:
  • the bottom-up approach consists in first looking at the smaller sub problems, and then solve the large subproblems using the solution to the smaller problems
  • the top down approach consists in solving the problem in a “natural manner” and check if you have calculated the solution the cur problem’s sub problem before. //does this means we check and check until the bottom, and now we have some kind of “base case”(it’s essentially the solution of sub problem, but it is the bottom), then we can just back tracking the solution of whole problem.
  • sometimes, post order tranversal related problem need a global variable for the final results instead of set the it as a parameter of recursion function. LC124 LC250. and most of the post order tranversal problem, the helper function do not just return void.