二叉树的遍历
以下代码以遍历二叉树并将其值加入list中返回为例
//节点代码
public class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode() {
}
TreeNode(int val) {
this.val = val; }
TreeNode(int val, TreeNode left, TreeNode right) {
this.val = val;
this.left = left;
this.right = right;
}
}
前序遍历
递归:
List<Integer> res=new ArrayList<>();
public List<Integer> preOrderIteration(TreeNode root) {
if(root==null){
return res;
}
res.add(root.val);
preOrderRecur(root.left);
preOrderRecur(root.right);
return res;
}
非递归:
先序遍历:中–左--右
使用栈,先根节点入栈,因为栈是先进后出,我们接下来要先遍历左子树,在遍历右子树,所以右子树根节点先入栈后遍历
,左子树根节点后入栈先遍历
。
由于先遍历根节点,所以用完弹栈即可。
public List<Integer> preOrderIteration(TreeNode root){
List<Integer> res=new ArrayList<>();
if(root==null)
return res;
Stack<TreeNode> stack=new Stack<>();
//加入根节点
stack.push(root);
while(!stack.isEmpty()){
//弹出根节点,并加入res
TreeNode node=stack.pop();
res.add(node.val);
//根据先进后出原则,先加入右子树的根节点
if(node.right!=null)
stack.push(node.right);
//加入左子树根节点
if(node.left!=null)
stack.push(node.left);
//返回while后弹出左子树根节点,继续相同操作
}
return res;
}
中序遍历
递归:
List<Integer> res=new ArrayList<>();
public List<Integer> inOrderIteration(TreeNode root) {
if(root==null){
return res;
}
preOrderRecur(root.left);
res.add(root.val);
preOrderRecur(root.right);
return res;
}
非递归:
中序遍历:左–中--右
想象遍历顺序,我们要从最左端子节点遍历,必然要经过其上层所有父节点,我们要入栈保留
,因为我们还要通过上层父节点到达其右子树进行遍历。
public List<Integer> inOrderIteration(TreeNode root) {
List<Integer> res=new ArrayList<>();
if(root==null)
return res;
Stack<TreeNode> stack=new Stack<>();
TreeNode cur=root;
while(!stack.isEmpty()||cur!=null){
//我们要先遍历最左侧节点,所以要对它的所有父节点入栈
while(cur!=null){
stack.push(cur);
cur=cur.left;
}
//弹出上层父节点,加入res
//此时遍历完了以当前node为父节点的左子树了
TreeNode node=stack.pop();
res.add(node.val);
//同时根据弹出的根节点,查看其右子树
//返回内层while之后继续按左中右的顺序遍历
if(node.right!=null){
//!=null表示存在右子树
cur=node.right;
}
//如果没有右子树,此时cur还是为null,所以不用担心返回后会进入内层while,造成重复入栈
}
return res;
}
后序遍历
递归:
List<Integer> res=new ArrayList<>();
public List<Integer> postOrderIteration(TreeNode root) {
if(root==null){
return res;
}
preOrderRecur(root.left);
preOrderRecur(root.right);
res.add(root.val);
return res;
}
非递归:
后序遍历:左–右--中
后序遍历难点在于,要对根节点弹栈两次(也就是入栈两次)
,第一次是为根据它得到右子树,第二次才是为了遍历其本身。所以我们要创建pre节点标识这是第几次出栈
。
最好在纸上画一画,会更好理解
public List<Integer> postorderTraversal(TreeNode root) {
List<Integer> res=new ArrayList<>();
if(root==null){
return res;
}
Stack<TreeNode> stack=new Stack<>();
TreeNode pre=null;
TreeNode cur=root;
while(cur!=null||!stack.isEmpty()){
//这里同中序遍历
//我们要先遍历最左侧节点,所以要对它的所有父节点入栈
while(cur!=null){
stack.push(cur);
cur=cur.left;
}
cur=stack.pop();
//根节点会出栈两次
//per作为判断当前节点右子树是否遍历过的标记(如果遍历过右子树,pre保留的就是当前节点的右子树)
//当root.right==prev,说明根节点的右子树已经遍历过了,根节点可以弹出(第二次弹出)
//root.right==null说明当前节点没有右子树,不用遍历右子树,当然可以弹出(不必进行二次入栈)
if(cur.right==null||cur.right==pre){
res.add(cur.val);
pre=cur;//表示以root为根节点的子树遍历完成
cur=null;//当前节点左子树已经入过栈,设置成null防止返回内层while后重复入栈
}else{
//当前根节点右子树没有遍历过,需要再次入栈
stack.push(cur);
cur=cur.right;//返回到外层while,遍历右子树
}
}
return res;
}
层序遍历
层序遍历:从上至下,每层从左到右
因为每次都要从左到右,所以要使用队列,先进先出
//遍历每层并装入List,将所有层在封装到总的list中
public List<Integer> levelTraversal(TreeNode root) {
List<List<Integer>> res = new ArrayList<>();
Queue<TreeNode> queue = new ArrayDeque<>();
if (root != null) {
queue.add(root);
}
while (!queue.isEmpty()) {
//当前层长度
int n = queue.size();
//用来记录当前层
List<Integer> level = new ArrayList<>();
//遍历当前层
for (int i = 0; i < n; i++) {
//弹出当前层节点并加入list
TreeNode node = queue.poll();
level.add(node.val);
//将下一层节点加入,为下一层遍历做准备
if (node.left != null) {
queue.add(node.left);
}
if (node.right != null) {
queue.add(node.right);
}
}
//将当前层加入总的list中
res.add(level);
}
return res;
}