如果说让你写出二叉树的前中后序遍历,你可能不假思索的使用递归的方式一分钟就能搞定!但是如果说使用O(1)空间复杂度实现呢?好了,废话不多说,他就是著名的mirror算法,通过使用指针的方式最多遍历两次二叉树即可实现二叉树的前中后序遍历。
注:递归和非递归实现二叉树的遍历,时间复杂度O(n),空间复杂度O(h) h树的高度。
mirror算法
流程
1. curr指向根节点
2. curr的左子树为空 curr = curr.right
3. curr的左子树不为空 找到左子树的最右边界 farRight,
3.1如果farRight为null,将farRight指向curr, curr=curr.left
3.2如果farRight不为null,farRight=null,curr=curr.right
特点:
- 如果一个节点有左子树那么他将会被遍历两次
mirror序:1 2 4 2 5 1 3 6 3 7
前序遍历:1 2 4 5 3 6 7
中序遍历:4 2 5 1 6 3 7
后续遍历:4 5 2 6 7 3 1
递归实现
public void preOrder(Node root) {
if (root == null) {
return;
}
System.out.printf("%d ", root.val);
preOrder(root.left);
preOrder(root.right);
}
public void inOrder(Node root) {
if (root == null) {
return;
}
inOrder(root.left);
System.out.printf("%d ", root.val);
inOrder(root.right);
}
public void postOrder(Node root) {
if (root == null) {
return;
}
postOrder(root.left);
postOrder(root.right);
System.out.printf("%d ", root.val);
}
非递归实现
借助栈,详细代码可在评论区留言。看到后会联系你;
mirror算法
/**
* mirror算法实现二叉树前序遍历,不打印第二次回到自己的节点
* @param root 根节点
*/
public void preOrderMirror(Node root) {
if (root == null) {
return;
}
Node curr = root;
while (curr != null) {
//左子树不为空,找到左子树最右边界
Node farRight = curr.left;
if (farRight != null) {
while (farRight.right != null && farRight.right != curr) {
farRight = farRight.right;
}
//最右边界指向null,curr左移,最右边界指向curr
if (farRight.right == null) {
farRight.right = curr;
System.out.printf("%d ", curr.val);
curr = curr.left;
}else{
//最右边界指向curr,最右边界指向null同时curr右移 此时为第二次回到curr节点,前序遍历不打印
farRight.right = null;
curr = curr.right;
}
}else{
//左子树为空,curr右移
System.out.printf("%d ", curr.val);
curr = curr.right;
}
}
}
/**
* mirror算法实现二叉树中序遍历,不打印有左子树且第一次遇到自己的节点
* @param root 根节点
*/
public void inOrderMirror(Node root) {
if (root == null) {
return;
}
Node curr = root;
while (curr != null) {
//左子树不为空,找到左子树最右边界
Node farRight = curr.left;
if (farRight != null) {
while (farRight.right != null && farRight.right != curr) {
farRight = farRight.right;
}
//最右边界指向null,curr左移,最右边界指向curr
if (farRight.right == null) {
farRight.right = curr;
curr = curr.left;
}else{
//最右边界指向curr,最右边界指向null同时curr右移 此时为第二次回到curr节点,前序遍历不打印
System.out.printf("%d ", curr.val);
farRight.right = null;
curr = curr.right;
}
}else{
//左子树为空,curr右移
System.out.printf("%d ", curr.val);
curr = curr.right;
}
}
}
/**
* mirror算法实现二叉树后序遍历,将第二次遇到自己的左树右边界逆序打印(反转链表)
* @param root 根节点
*/
public void postOrderMirror(Node root) {
if (root == null) {
return;
}
Node curr = root;
while (curr != null) {
//左子树不为空,找到左子树最右边界
Node farRight = curr.left;
if (farRight != null) {
while (farRight.right != null && farRight.right != curr) {
farRight = farRight.right;
}
//最右边界指向null,curr左移,最右边界指向curr
if (farRight.right == null) {
farRight.right = curr;
curr = curr.left;
}else{
//最右边界指向curr,最右边界指向null同时curr右移 此时为第二次回到curr节点,前序遍历不打印
farRight.right = null;
printEdge(curr.left);
curr = curr.right;
}
}else{
//左子树为空,curr右移
curr = curr.right;
}
}
printEdge(root);
}
public static void printEdge(Node root) {
Node tail = reverseEdge(root);
Node cur = tail;
while (cur != null) {
System.out.print(cur.val + " ");
cur = cur.right;
}
reverseEdge(tail);
}
public static Node reverseEdge(Node from) {
Node pre = null;
Node next;
while (from != null) {
next = from.right;
from.right = pre;
pre = from;
from = next;
}
return pre;
}
talk is cheap ,show me your code ! ,文章有疑问可在下方留言,简单的问题有更好的解法,你便不简单!面试的时候又可以跟面试官吹一波了。
参考:
左程云