二叉树遍历

二叉树的前序中序后序

前序:中,左,右
中序:左(大小树都是找最左),中,右
后序:左,中,右

遍历二叉树的三种方法

史上最全遍历二叉树详解
详细通俗的思路分析,多解法

递归解法
  • 前序遍历
public static void preOrderRecur(TreeNode head) {
    if (head == null) {
        return;
    }
    // 调用一次postOrderRecur,就代表调用了一棵树
    System.out.print(head.value + " "); // 输出根节点
    preOrderRecur(head.left);	// 不断找左子树(即打印左子树),此时树不断变小
    preOrderRecur(head.right);	// 不断找右子树(即打印右子树),此时树不断增大
}

  • 中序遍历
public static void preOrderRecur(TreeNode head) {
    if (head == null) {
        return;
    }
    // 调用一次postOrderRecur,就代表调用了一棵树
    preOrderRecur(head.left);	// 不断找左子树(即打印左子树),此时树不断变小
    System.out.print(head.value + " "); // 输出根节点
    preOrderRecur(head.right);	// 不断找右子树(即打印右子树),此时树不断增大
}
  • 后序遍历
public static void postOrderRecur(TreeNode head) {
    if (head == null) {
        return;
    }
    // 调用一次postOrderRecur,就代表调用了一棵树
    postOrderRecur(head.left);	// 不断找左子树(即打印左子树)
    postOrderRecur(head.right);	// 不断找右子树(即打印右子树)
    System.out.print(head.value + " ");	// 打印根节点
}

迭代解法

首先我们应该创建一个Stack用来存放节点,首先我们想要打印根节点的数据,此时Stack里面的内容为空,所以我们优先将头结点加入Stack,然后打印。

之后我们应该先打印左子树,然后右子树。所以先加入Stack的就是右子树,然后左子树。
此时你能得到的流程如下:
在这里插入图片描述

  • 前序遍历
public static void preOrderIteration(TreeNode head) {
	if (head == null) {
		return;
	}
	Stack<TreeNode> stack = new Stack<>();
	stack.push(head);
	// 利用栈来回溯到上一个根节点
	while (!stack.isEmpty()) {
		TreeNode node = stack.pop();	// 弹出栈顶元素,将其作为根节点
		
		System.out.print(node.value + " ");	// 输出根节点
		if (node.right != null) {
			stack.push(node.right);	// 压入右节点
		}
		if (node.left != null) {
			stack.push(node.left);	// 压入左节点
		}
	}
}
  • 中序遍历
// 思路:先把不断将左节点压入栈,再依次处理,每次处理一个栈中的“左节点”都是将其作为"cur"对待。
public static void inOrderIteration(TreeNode head) {
	if (head == null) {
		return;
	}
	TreeNode cur = head;
	Stack<TreeNode> stack = new Stack<>();
	// 利用栈来回溯到上一个根节点
	while (!stack.isEmpty() || cur != null) {
		// 不断找当前节点的左节点,压入栈,并更新当前节点,直到叶节点位置(最左),此时cur == 叶节点.left == null。
		while (cur != null) {
			stack.push(cur);
			cur = cur.left;
		}
		// 当成"cur"来对待
		// 此处可以先以最左节点的角度来看
		// 先输出左节点a。拿到左节点a(即"cur"),表示左节点a的左节点已经处理完了。
		TreeNode node = stack.pop();
		System.out.print(node.value + " ");
		// 再处理左节点a(即"cur")的右节点(右子树),右子树也可能存在“左”,所以要先处理这个右子树
		if (node.right != null) {
			// 注意:假如此时node是最左节点,node.right存在,node.right可能有node.right.left,但不是整个大树的最左节点
			cur = node.right;
		}
	}
}
  • 后序
    注意:拿到最左节点,也不可能马上输出值,还要先判断最左节点的右子树是否存在。这里就导致了最左节点的值不应该从栈中pop出来,因为一旦pop出来了,最左节点右子树也处理完了,轮到最左节点输出了,却找不到了。
    在这里插入图片描述
public List<Integer> postorderTraversal(TreeNode root) {
    List<Integer> list = new ArrayList<>();
    Stack<TreeNode> stack = new Stack<>();
    Set<TreeNode> set = new HashSet<>(); // 整个set是为了记录已经处理完的节点
    TreeNode cur = root;
    while (cur != null || !stack.isEmpty()) {
    	//节点不为空一直压栈
        while (cur != null && !set.contains(cur)) {
            stack.push(cur);
            cur = cur.left;
        }
        // 先把栈顶节点复制出来
        cur = stack.peek();
        //右子树为空或者第二次来到这里
        if (cur.right == null || set.contains(cur)) {
            list.add(cur.val);
            set.add(cur);
            stack.pop();//将当前节点弹出
            if (stack.isEmpty()) {
                return list;
            }
            //转到右子树,这种情况对应于右子树为空的情况,如果没有set,则会重复来到此处
            cur = stack.peek();
            cur = cur.right;
        //从左子树过来,加到 set 中,转到右子树
        } else {
            set.add(cur);
            cur = cur.right;
        }
    }
    return list;
}

Morris解法

Morris遍历使用二叉树节点中大量指向null的指针,由Joseph Morris 于1979年发明。
时间复杂度:O(n)O(n)
额外空间复杂度:O(1)O(1)

在你阅读以下代码之前,在这边先讲解一下Morris的通用解法过程。
在这里插入图片描述
Morris的整体思路就是将 以某个根结点开始,找到它左子树的最右侧节点之后与这个根结点进行连接
我们可以从 图2 看到,如果这么连接之后,cur 这个指针是可以完整的从一个节点顺着下一个节点遍历,将整棵树遍历完毕,直到 7 这个节点右侧没有指向。

public static void preOrderMorris(TreeNode head) {
	if (head == null) {
		return;
	}
	TreeNode cur1 = head;//当前开始遍历的节点
	TreeNode cur2 = null;//记录当前结点的左子树
	while (cur1 != null) {
		cur2 = cur1.left;
		if (cur2 != null) {
			while (cur2.right != null && cur2.right != cur1) {//找到当前左子树的最右侧节点,且这个节点应该在指向根结点之前,否则整个节点又回到了根结点。
				cur2 = cur2.right;
			}
			if (cur2.right == null) {//这个时候如果最右侧这个节点的右指针没有指向根结点,创建连接然后往下一个左子树的根结点进行连接操作。
				cur2.right = cur1;
				cur1 = cur1.left;
				continue;
			} else {//当左子树的最右侧节点有指向根结点,此时说明我们已经回到了根结点并重复了之前的操作,同时在回到根结点的时候我们应该已经处理完 左子树的最右侧节点 了,把路断开。
				cur2.right = null;
			}
		} 
		cur1 = cur1.right;//一直往右边走,参考图。这个右是回到根节点处,继续看循环,又从根节点出发,当遇到if (cur2.right == null)时不成立,去到else断开与根节点的连接。最后再次来到此处,往根节点的右走,因为此时左已经处理完了。
	}
}
  • 前序
public static void preOrderMorris(TreeNode head) {
	if (head == null) {
		return;
	}
	TreeNode cur1 = head;
	TreeNode cur2 = null;
	while (cur1 != null) {
		cur2 = cur1.left;
		if (cur2 != null) {
			while (cur2.right != null && cur2.right != cur1) {
				cur2 = cur2.right;
			}
			if (cur2.right == null) {
				cur2.right = cur1;
				System.out.print(cur1.value + " ");
				cur1 = cur1.left;
				continue;
			} else {
				cur2.right = null;
			}
		} else {
			System.out.print(cur1.value + " ");
		}
		cur1 = cur1.right;
	}
}
  • 中序
public static void inOrderMorris(TreeNode head) {
	if (head == null) {
		return;
	}
	TreeNode cur1 = head;
	TreeNode cur2 = null;
	while (cur1 != null) {
		cur2 = cur1.left;
		//构建连接线
		if (cur2 != null) {
			while (cur2.right != null && cur2.right != cur1) {
				cur2 = cur2.right;
			}
			if (cur2.right == null) {
				cur2.right = cur1;
				cur1 = cur1.left;
				continue;
			} else {
				cur2.right = null;
			}
		}
		System.out.print(cur1.value + " ");
		cur1 = cur1.right;
	}
}
  • 后序
    -
    当我们到达最左侧,也就是左边连线已经创建完毕了。
    打印 4
    打印 5 2
    打印 6
    打印 7 3 1
    我们将一个节点的连续右节点当成一个单链表来看待。
    当我们返回上层之后,也就是将连线断开的时候,打印下层的单链表。
    比如返回到 2,此时打印 4
    比如返回到 1,此时打印 5 2
    比如返回到 3,此时打印 6
    那么我们只需要将这个单链表逆序打印就行了,下文也给出了 单链表逆序代码
    这里不应该打印当前层,而是下一层,否则根结点会先与右边打印。

//后序Morris
public static void postOrderMorris(TreeNode head) {
	if (head == null) {
		return;
	}
	TreeNode cur1 = head;//遍历树的指针变量
	TreeNode cur2 = null;//当前子树的最右节点
	while (cur1 != null) {
		cur2 = cur1.left;
		if (cur2 != null) {
			while (cur2.right != null && cur2.right != cur1) {
				cur2 = cur2.right;
			}
			if (cur2.right == null) {
				cur2.right = cur1;
				cur1 = cur1.left;
				continue;
			} else {
				cur2.right = null;
				postMorrisPrint(cur1.left);
			}
		}
		cur1 = cur1.right;
	}
	postMorrisPrint(head);
}
//打印函数
public static void postMorrisPrint(TreeNode head) {
	TreeNode reverseList = postMorrisReverseList(head);
	TreeNode cur = reverseList;
	while (cur != null) {
		System.out.print(cur.value + " ");
		cur = cur.right;
	}
	postMorrisReverseList(reverseList);
}
//翻转单链表
public static TreeNode postMorrisReverseList(TreeNode head) {
	TreeNode cur = head;
	TreeNode pre = null;
	while (cur != null) {
		TreeNode next = cur.right;
		cur.right = pre;
		pre = cur;
		cur = next;
	}
	return pre;
}

关于递归

运用递归解决树的问题

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值