Morris莫里斯遍历
程序员代码面试指南(第2版)第3章 二叉树问题:遍历二叉树的神级方法
https://leetcode.com/articles/binary-tree-inorder-traversal/
Step 1: Initialize current as root
Step 2: While current is not NULL,
If current does not have left child
a. Add current’s value b. Go to the right, i.e., current = current.right
Else
If rightmost node of current's left subtree has no right child
a. In current's left subtree, make current the right child of the rightmost node b. Go to this left child, i.e., current = current.left
Else
a. Restore the origianl state, assign null value to the right child of the rightmost node
在了解Morris序后,通过对能到达2次的节点选择性的打印,可以很直观的得到前序遍历和中序遍历。
对于Morris序,先把左子树的右边界节点和cur连上,再处理左子树。(这个查找左子树右边界节点的过程不属于Morris遍历过程。)
cur被指针保存后,就可以放心的移动了。
有左子树的节点都会到达2次,每次都需要重新查找右边界。
Morris遍历
public static void morris(Node head) {
if (head == null) {
return;
}
Node cur = head;
//这个mostRight指的是左子树的mostRight,而不是当前节点的mostRight
Node mostRight = null;
while (cur != null) {
mostRight = cur.left;
if (mostRight != null) { // 如果当前cur有左子树
// 找到cur左子树上最右的节点
while (mostRight.right != null && mostRight.right != cur) {
mostRight = mostRight.right;
}
// 从上面的while里出来后,mostRight就是cur左子树上最右的节点
if (mostRight.right == null) { // 如果mostRight.right是指向null的
mostRight.right = cur; // 让其指向cur
cur = cur.left; // cur向左移动
continue; // 回到最外层的while,继续判断cur的情况
} else { // 如果mostRight.right是指向cur的
mostRight.right = null; // 让其指向null
}
}
// cur如果没有左子树,cur向右移动
// 或者cur左子树上最右节点的右指针是指向cur的,cur向右移动
cur = cur.right;
}
}
Morris前序遍历
对于只到达1次的节点,马上打印。
对于到达2次的节点,只打印第1次。
public static void morrisPre(Node head) {
if (head == null) {
return;
}
Node cur = head;
Node mostRight = null;
while (cur != null) {
mostRight = cur.left;
if (mostRight != null) {
//到达2次的节点
while (mostRight.right != null && mostRight.right != cur) {
mostRight = mostRight.right;
}
if (mostRight.right == null) {
//2次中的第1次
mostRight.right = cur;
System.out.print(cur.value + " ");
cur = cur.left;
continue;
} else {
//2次中的第2次
mostRight.right = null;
}
} else {
//只能到达1次的节点
System.out.print(cur.value + " ");
}
cur = cur.right;
}
System.out.println();
}
Morris中序遍历
对于只到达1次的节点,马上打印。
对于到达2次的节点,只打印第2次。
public static void morrisIn(Node head) {
if (head == null) {
return;
}
Node cur = head;
Node mostRight = null;
while (cur != null) {
mostRight = cur.left;
if (mostRight != null) {
while (mostRight.right != null && mostRight.right != cur) {
mostRight = mostRight.right;
}
if (mostRight.right == null) {
mostRight.right = cur;
cur = cur.left;
continue;
} else {
mostRight.right = null;
}
}
//只能到达1次的节点和能到达2次中的节点的第2次,会经过这里
System.out.print(cur.value + " ");
cur = cur.right;
}
System.out.println();
}
Morris后序遍历
对于第二次遇到的节点,打印其左子树的右边界。
最后打印整棵树的右边界。
public static void morrisPos(Node head) {
Node cur = head;
Node mostRight = null;
while (cur != null) {
mostRight = cur.left;
if (mostRight != null) {
while(mostRight.right != null && mostRight.right != cur) {
mostRight = mostRight.right;
}
if (mostRight.right == null) {
mostRight.right = cur;
cur = cur.left;
continue;
} else {
mostRight.right = null;
traverseRightEdge(head);
}
}
cur = cur.right;
}
traverseRightEdge(head);
} private static void traverseRightEdge(Node head) {
Node tail = reverseRightEdge(head);
for (Node cur = tail; cur != null; cur = cur.right) {
doSomething(cur);
}
reverseRightEdge(tail);
} private static Node reverseRightEdge(Node head) {
Node newHead = null;
Node next = null;
while (head != null) {
next = head.right;
head.right = newHead;
newHead = head;
head = next;
}
return newHead;
} private static void doSomething() {}
Morris莫里斯遍历的更多相关文章
- 数据结构《13》----二叉树 Morris 前序遍历
三种二叉树的后序遍历的方法: 1. 递归 O(n) 时间复杂度, O(n) 空间复杂度 2. 迭代(用栈) O(n) 时间复杂度, O(n) 空间 ...
- 二叉树遍历,递归,栈,Morris
一篇质量非常高的关于二叉树遍历的帖子,转帖自http://noalgo.info/832.html 二叉树遍历(递归.非递归.Morris遍历) 2015年01月06日 | 分类:数据结构 | 标 ...
- 《程序员代码面试指南》第三章 二叉树问题 遍历二叉树的神级方法 morris
题目 遍历二叉树的神级方法 morris java代码 package com.lizhouwei.chapter3; /** * @Description:遍历二叉树的神级方法 morris * @ ...
- Morris 遍历实现二叉树的遍历
Morris 遍历实现二叉树的遍历 作者:Grey 原文地址: 博客园:Morris 遍历实现二叉树的遍历 CSDN:Morris 遍历实现二叉树的遍历 说明 Morris 遍历可以实现二叉树的先,中 ...
- [Alg] 二叉树的非递归遍历
1. 非递归遍历二叉树算法 (使用stack) 以非递归方式对二叉树进行遍历的算法需要借助一个栈来存放访问过得节点. (1) 前序遍历 从整棵树的根节点开始,对于任意节点V,访问节点V并将节点V入栈, ...
- 二叉树的N中遍历方式和拓展应用
(一)创建二叉树,如下图所示,一个标准的二叉树是所有位于根节点的左侧的子节点都是比根节点小的,右侧则都是大于根节点的. public class BinaryNode { public int val ...
- Leetcode 笔记 99 - Recover Binary Search Tree
题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...
- [LeetCode] Binary Tree Preorder/Inorder/Postorder Traversal
前中后遍历 递归版 /* Recursive solution */ class Solution { public: vector<int> preorderTraversal(Tree ...
- 树及其衍生算法(Trees and tree algorithms)
1,二叉树(Binary tree) 二叉树:每一个节点最多两个子节点,如下图所示: 相关概念:节点Node,路径path,根节点root,边edge,子节点 children,父节点parent,兄 ...
随机推荐
- 【uva 1612】Guess(算法效率,2种想法)
题意:已知 N 位选手的3题的预期得分,得分要不全拿,要不为0.且知道最后的实际名次,而且得分相同的选手,ID小的排在前面.问这样的名次可能吗.若可能,输出最后一名的最高可能得分.(N≤16384) ...
- Codeforces Round #343 (Div. 2) E. Famil Door and Roads (树形dp,lca)
Famil Door's City map looks like a tree (undirected connected acyclic graph) so other people call it ...
- FZU1894 志愿者选拔
Problem Description 世博会马上就要开幕了,福州大学组织了一次志愿者选拔活动.参加志愿者选拔的同学们排队接受面试官们的面试.参加面试的同学们按照先来先面试并且先结束的原则接受面试官们 ...
- Codeforces Round #544 (Div. 3) E. K Balanced Teams (DP)
题意:有\(n\)个人,每个人的能力值是\(a_i\),现在你想将这些人分成\(k\)组(没必要全选),但是每组中最高水平和最低水平的人的能力差值必须\(\le 5\),问最多能选多少人. 题解:想了 ...
- Codeforces Round #498 (Div. 3) D. Two Strings Swaps (思维)
题意:给你两个长度相同的字符串\(a\)和\(b\),你可以将相同位置上的\(a\)和\(b\)的字符交换,也可以将\(a\)或\(b\)中某个位置和对应的回文位置上的字符交换,这些操作是不统计的,你 ...
- 再记一次 应用服务器 CPU 暴高事故分析
一:背景 1. 前言 大概有2个月没写博客了,不是不想写哈
- Dubbo和SpringCloud的优劣势比较--总体架构
从整体架构上来看 二者模式接近,都需要服务提供方,注册中心,服务消费方.差异不大.详见下方: Dubbo Provider: 暴露服务的提供方,可以通过jar或者容器的方式启动服务 Consumer: ...
- 【原创】docker & kubernetes问题总结
1.entrypoint & cmd 指令的区别 这主要考察 Dockerfile 良好实践中关于容器启动时运行的命令. entrypoint 和 cmd 命令都是设置容器启动时要执行的命令, ...
- python之字符串方法upper/lower
1.描述: upper():用于将字符串全部转换为大写字母 lower():用于将字符串全部转换为小写字母 2.语法 str.upper() str.lower() 3.返回值 upper()或low ...
- 卸载vue2.9.6版本,安装新版本
1.检查vue安装目录(cmd中输入) where vue 2.删除目录中的关于vue的文件(可以将文件按时间排序,找到vue相关的文件删除) 3.检查vue是否还能找到 4.安装新版本的vue np ...