二叉树的遍历
二叉树遍历分为先序遍历,中序遍历和后序遍历,遍历顺序如下:
- 先序遍历,遍历顺序为根节点,左子树,右子树。
- 中序遍历,遍历顺序为左子树,根节点,右子树。
- 后序遍历,遍历顺序为左子树,右子树,根节点。
二叉树的遍历实现方法分为递归和非递归。
递归遍历
递归遍历代码比较简洁,按照上述的顺序调用即可。
先序遍历
private List<Integer> results = new ArrayList<>();
public void travesal(TreeNode root) {
if (root == null) return;
results.add(root.val);
travesal(root.left);
travesal(root.right);
}
中序遍历
private List<Integer> results = new ArrayList<>();
public void travesal(TreeNode root) {
if (root == null) return;
travesal(root.left);
results.add(root.val);
travesal(root.right);
}
后序遍历
private List<Integer> results = new ArrayList<>();
public void travesal(TreeNode root) {
if (root == null) return;
travesal(root.left);
travesal(root.right);
results.add(root.val);
}
递归方法的核心就是栈,每个函数都有自己栈帧,即使函数自身调用自身也会生成一个栈帧。
每个栈帧都会存储变量和返回地址。
当二叉树进行遍历时,会将节点变量都会保存在栈帧中,直到整个函数执行完毕时,才会将节点从栈帧中弹出。
比如如下最简单的树
调用顺序为如下所示。
travesal(root) ->
travesal(root.left) -> (travesal(root.left.left) == null) return
-> travesal(root.left.right == null) return -> travesal(root.left) 执行结束return
travesal(root.right) -> (travesal(root.right.left) == null) return
-> travesal(root.right.right == null) return -> travesal(root.right) 执行结束return
-> travesal(root)执行结束,程序结束
计算机中整个函数的栈帧如下所示
可以看到上面的递归方法,先把左节点压栈,再把左子树的节点依次向下压栈,直到节点为空,切换为右节点,然后把右子树的节点依次向下压栈,直到为空,直到当前节点下的左右子树遍历完毕。以此类推当所有节点均被遍历,递归函数执行完毕,栈即为空。
非递归二叉树遍历
非递归调用核心还是利用栈,就是模拟递归栈帧。
被调用函数栈帧保存了上个调用函数的返回地址,可以知道每个函数执行状态,是执行到左节点被压入、右节点被压入、还是弹出栈。
在先序遍历中,results.add(root.val)
,将自身节点存储值保存到list中,即根节点被遍历保存。
然后在函数中连续调用了自身函数两次,分别为travesal(root.left)
和travesal(root.right)
,即先将左右节点压入到栈中。
以先序遍历为例,先保存root节点的值,再遍历完左子树,最后遍历右子树。
根据栈的规则,先进后出,所以非递归函数中使用栈,先压入root右节点,再压入root左节点,最后压入root节点。
如果栈为空,则整个二叉树遍历完成。
实现思路
我们根据上面的思路,实现代码如下所示
public List<Integer> preorderTraversal(TreeNode root) {
if (root == null) return Collections.emptyList();
Stack<TreeNode> stack = new Stack<TreeNode>();
List<Integer> results = new ArrayList<>();
stack.push(root);
while(!stack.isEmpty()) {
TreeNode top = stack.pop();
if (top != null) {
if (top.right != null) stack.push(top.right);
if (top.left != null) stack.push(top.left);
stack.push(top);
stack.push(null);
} else {
results.add(stack.pop().val);
}
}
return results;
}
-
root节点压入到栈中,再弹出,并判断
top
是否为null
。 -
如果
top
为null
则表示当前节点执行结束,可以弹出栈保存结果,表示非递归部分 -
如果
top
不为null
则表示需要将top
的右节点和左节点压入到栈中,相当于执行travesal(root.right)
和travesal(root.left)
方法,表示递归部分。 -
if(top!=null)部分
,判断root.left
和root.right
左右节点是否为空,不为空则压入到栈中。
最后再压入root
节点,压入null
。null
作为标志位,说明root节点遍历完毕,需要被弹出栈保存它的值。
6.if(top==null)
弹出root,值保存到results
中。
如下图所示,按照非递归代码逻辑,展示了最简单的树先序遍历过程。
非递归中序遍历和后序遍历我们只要和递归一样变换之间的顺序即可。
非递归中序遍历
和先序遍历相比,将stack.push(top) stack.push(null)
位置放到了中间,和递归遍历中把results.add(value)
放到中间类似。
public List<Integer> postorderTraversal(TreeNode root) {
if (root == null) return Collections.emptyList();
Stack<TreeNode> stack = new Stack<TreeNode>();
List<Integer> results = new ArrayList<>();
stack.push(root);
while(!stack.isEmpty()) {
TreeNode top = stack.pop();
if (top != null) {
if (top.right != null) stack.push(top.right);
stack.push(top);
stack.push(null);
if (top.left != null) stack.push(top.left);
} else {
results.add(stack.pop().val);
}
}
return results;
}
按照上面非递归中序遍历的逻辑,整个过程如下所示,简化了部分步骤,整体思想不变。
非递归后序遍历
和先序遍历相比,将stack.push(top) stack.push(null)
放到了最前面,先进后出,相当于递归函数中results.add(value)
放到了最后面。
public List<Integer> postorderTraversal(TreeNode root) {
if (root == null) return Collections.emptyList();
Stack<TreeNode> stack = new Stack<TreeNode>();
List<Integer> results = new ArrayList<>();
stack.push(root);
while(!stack.isEmpty()) {
TreeNode top = stack.pop();
if (top != null) {
stack.push(top);
stack.push(null);
if (top.right != null) stack.push(top.right);
if (top.left != null) stack.push(top.left);
} else {
results.add(stack.pop().val);
}
}
return results;
}