程序员代码面试指南(第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. HDU6661 Acesrc and String Theory【SA】

    Acesrc and String Theory Problem Description Acesrc is a famous string theorist at Nanjing Universit ...

  2. 2019牛客多校 Round7

    Solved:5 Rank:296 E Find the median (线段树) 题意:最开始一个空的数组 4e5次操作 每次把Li,Ri中的每个数插入进来 问当前的中位数 题解:把这n个区间离散化 ...

  3. Codeforces Round #498 (Div. 3) D. Two Strings Swaps (思维)

    题意:给你两个长度相同的字符串\(a\)和\(b\),你可以将相同位置上的\(a\)和\(b\)的字符交换,也可以将\(a\)或\(b\)中某个位置和对应的回文位置上的字符交换,这些操作是不统计的,你 ...

  4. 牛客算法周周练20 F.紫魔法师 (二分图染色)

    题意:给你一张图,对其染色,使得相连的点的颜色两两不同求,最少使用多少种颜色. 题解:首先,若\(n=1\),只需要一种.然后我们再去判断是否是二分图,对于二分图,两种颜色就够了,若不是二分图,也就是 ...

  5. HttpClient&&RestTemplate学习

    1. 什么是HttpClient HttpClient是Apache下面的子项目,可以提供高效的,最新的,功能丰富的支持HTTP协议的客户端编程工具包. 2. 为什么要学习HttpClient Htt ...

  6. dll的注册与反注册

    regsvr32.exe是32位系统下使用的DLL注册和反注册工具,使用它必须通过命令行的方式使用,格式是:regsvr32 [/i[:cmdline]] DLL文件名命令可以在"开始→运行 ...

  7. woj1010 alternate sum 数学 woj1011 Finding Teamates 数学

    title: woj1010 alternate sum 数学 date: 2020-03-10 categories: acm tags: [acm,woj,数学] 一道数学题.简单. 题意 给一个 ...

  8. C++ part9

    1.静态多态和动态多态 静态多态:函数重载,模板.编译期间完成. 动态多态:虚函数.运行期间实现. 2.模板的实现和优缺点 函数模板的代码并不能直接编译成二进制代码,而是要实例出一个模板实例.写了模板 ...

  9. 在Python里,用股票案例讲描述性统计分析方法(内容来自我的书)

    描述性统计是数学统计分析里的一种方法,通过这种统计方法,能分析出数据整体状况以及数据间的关联.在这部分里,将用股票数据为样本,以matplotlib类为可视化工具,讲述描述性统计里常用指标的计算方法和 ...

  10. 重学c#————struct

    前言 简单整理一下struct. 正文 struct 对于struct 而言呢,我们往往会拿class作为对比,但是呢,我们在初学阶段用class来替代struct,struct的存在感越来越低了. ...