程序员代码面试指南(第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次,每次都需要重新查找右边界。

  1. 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;
    }
    }
  2. 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();
    }
  3. 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();
    }
  4. 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莫里斯遍历的更多相关文章

  1. 数据结构《13》----二叉树 Morris 前序遍历

    三种二叉树的后序遍历的方法: 1. 递归                      O(n) 时间复杂度, O(n) 空间复杂度 2. 迭代(用栈)       O(n) 时间复杂度, O(n) 空间 ...

  2. 二叉树遍历,递归,栈,Morris

    一篇质量非常高的关于二叉树遍历的帖子,转帖自http://noalgo.info/832.html 二叉树遍历(递归.非递归.Morris遍历) 2015年01月06日 |  分类:数据结构 |  标 ...

  3. 《程序员代码面试指南》第三章 二叉树问题 遍历二叉树的神级方法 morris

    题目 遍历二叉树的神级方法 morris java代码 package com.lizhouwei.chapter3; /** * @Description:遍历二叉树的神级方法 morris * @ ...

  4. Morris 遍历实现二叉树的遍历

    Morris 遍历实现二叉树的遍历 作者:Grey 原文地址: 博客园:Morris 遍历实现二叉树的遍历 CSDN:Morris 遍历实现二叉树的遍历 说明 Morris 遍历可以实现二叉树的先,中 ...

  5. [Alg] 二叉树的非递归遍历

    1. 非递归遍历二叉树算法 (使用stack) 以非递归方式对二叉树进行遍历的算法需要借助一个栈来存放访问过得节点. (1) 前序遍历 从整棵树的根节点开始,对于任意节点V,访问节点V并将节点V入栈, ...

  6. 二叉树的N中遍历方式和拓展应用

    (一)创建二叉树,如下图所示,一个标准的二叉树是所有位于根节点的左侧的子节点都是比根节点小的,右侧则都是大于根节点的. public class BinaryNode { public int val ...

  7. Leetcode 笔记 99 - Recover Binary Search Tree

    题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...

  8. [LeetCode] Binary Tree Preorder/Inorder/Postorder Traversal

    前中后遍历 递归版 /* Recursive solution */ class Solution { public: vector<int> preorderTraversal(Tree ...

  9. 树及其衍生算法(Trees and tree algorithms)

    1,二叉树(Binary tree) 二叉树:每一个节点最多两个子节点,如下图所示: 相关概念:节点Node,路径path,根节点root,边edge,子节点 children,父节点parent,兄 ...

随机推荐

  1. Codeforces Round #575 (Div. 3) F. K-th Path

    传送门 题意: 这道题把我看得懵懵的(不敢相信),其实就是给你n个点和m条边(无向图),你要找出来任意两点之间的的最短距离,然后再从其中找出来第k个最小值 题解: 正常思维就是floyd多源最短路算法 ...

  2. JavaScript——五

    onload:在加载的时候 因为网页代码是从上到下执行的,所以我们有些对网页内容的操作要先加载出网页内容后再执行script的内容,这个时候如果没有onload我们只能写在这些内容的后面,但是有了lo ...

  3. 行业动态 | 腾讯合作商Babylon使用Cassandra保护患者数据并提高医疗效果

    医疗世界正在快速朝向个性化和低成本的方向发展,Babylon Health看到了这样的机会--通过开创性的云端应用来大幅扩张和改进他们所提供的服务.   通过使用基于Apache Cassandra的 ...

  4. 【转】Redis数据备份和重启恢复

    一.对Redis持久化的探讨与理解 目前Redis持久化的方式有两种: RDB 和 AOF 首先,我们应该明确持久化的数据有什么用,答案是用于重启后的数据恢复.Redis是一个内存数据库,无论是RDB ...

  5. 计算机网络基础篇-ppp协议

    所谓的PPP协议是点对点协议,是目前使用最广泛的数据链路层的协议.大部分用户使用电话线拨号入网的,从用户计算机到ISP的链路所使用的数据链路层协议就是PPP协议. 首先介绍下拨号入网的过程.因特网服务 ...

  6. CodeForces 1047C Enlarge GCD(数论)题解

    题意:n个数的gcd是k,要你删掉最少的数使得删完后的数组的gcd > k 思路:先求出k,然后每个数除以k.然后找出出现次数最多的质因数即可. 代码: #include<cmath> ...

  7. React render algorithm & Fiber vs Stack

    React render algorithm & Fiber vs Stack React 渲染算法 & Fiber vs Stack https://stackoverflow.co ...

  8. 什么是 DNS 的 A记录 和 CNAME记录 域名解析 为我的自定义域名创建 CNAME 记录

    # CNAME https://support.google.com/blogger/answer/58317?hl=zh-Hans 为我的自定义域名创建 CNAME 记录 如果您的域名不是在 Blo ...

  9. koa url path & koa-router

    koa url path & koa-router url path & regex koa path router "use strict"; /** * * @ ...

  10. webpack 5

    webpack 5 webpack 5 requires at least Node.js 10.13.0 (LTS). https://webpack.js.org/migrate/5/ https ...