#include<iostream>
using namespace std; struct TreeNode{
int val;
TreeNode* right;
TreeNode* left;
TreeNode(int _val):val(_val),right(nullptr),left(nullptr){}
}; void Morris(TreeNode* root){
if(root == nullptr)
return;
TreeNode* cur=root;
TreeNode* mostRight=nullptr; //当cur为空停止,即上图中的情况3
while(cur != nullptr){
mostRight=cur->left;
//如果mostRight!=nullptr,进入情况2
if(mostRight != nullptr){
while(mostRight->right && mostRight != cur)
mostRight=mostRight->right;
//当mostRight == nullptr,即情况2.a
if(mostRight->right == nullptr){
mostRight->right=cur;
cur=cur->left;
continue;
}
else{ //当mostRight == cur,即情况2.b
mostRight->right=nullptr;
/*cur=cur->left;
continue;*/
}
}
//mostright为空进入情况1
cur=cur->left;
}
}

Morris前序遍历

有左子树就会回到cur节点两次,没有就来一次

void MorrisPre(TreeNode* root) {
if (root == nullptr)
return;
TreeNode* cur = root;
TreeNode* mostRight = nullptr; while (cur != nullptr) {
mostRight = cur->left; if (mostRight != nullptr) {
while (mostRight->right && mostRight->right != cur) {
mostRight = mostRight->right;
} if (mostRight->right == nullptr) {
//第一次到达左子树的最右子树
printf("%d ", cur->val);
mostRight->right = cur;
cur = cur->left;
continue;
}
else {
//第二次到达左子树的最右子树
mostRight->right == cur;
mostRight->right = nullptr;
/*cur = cur->right;
continue;*/
}
}
else {
//当前cur只能来一次
printf("%d ", cur->val);
} cur = cur->right;
}
}

Morris中序遍历

void MorrisIn(TreeNode* root) {
if (root == nullptr)
return;
TreeNode* cur = root;
TreeNode* mostRight = nullptr; while (cur != nullptr) {
mostRight = cur->left; if (mostRight != nullptr) {
while (mostRight->right && mostRight->right != cur) {
mostRight = mostRight->right;
} if (mostRight->right == nullptr) { //第一次到达左子树的最右子树
mostRight->right = cur;
cur = cur->left;
continue;
}
else { //第二次到达左子树的最右子树 mostRight->right == cur;
mostRight->right = nullptr;
/*cur = cur->right;
continue;*/
}
} //往右移动的时候打印
printf("%d ", cur->val);
cur = cur->right;
}
}

Morris后序遍历

//右孩子指向上一个节点
TreeNode* ReverseNode(TreeNode* node) {
TreeNode* pre = nullptr;
TreeNode* next = nullptr;
while (node) {
next = node->right;
node->right = pre;
pre = node;
node = next;
}
return pre;
} //逆序打印左孩子的右边界
void PrintNode(TreeNode* node) {
TreeNode* tail = ReverseNode(node);
TreeNode* cur = tail;
while (cur) {
printf("%d ", cur->val);
cur = cur->right;
}
ReverseNode(tail); //还原
} void MorrisPos(TreeNode* root) {
if (root == nullptr)
return;
TreeNode* cur = root;
TreeNode* mostRight = nullptr; while (cur != nullptr) {
mostRight = cur->left; if (mostRight != nullptr) {
while (mostRight->right && mostRight->right != cur) {
mostRight = mostRight->right;
} if (mostRight->right == nullptr) { //第一次到达左子树的最右子树
mostRight->right = cur;
cur = cur->left;
continue;
}
else { //第二次到达左子树的最右子树 mostRight->right == cur;
mostRight->right = nullptr; //逆序打印左子树的右边界
PrintNode(cur->left);
}
}
cur = cur->right;
} PrintNode(root);
}

Morris遍历以及Morris前序中序后序遍历实现的更多相关文章

  1. 算法进阶面试题03——构造数组的MaxTree、最大子矩阵的大小、2017京东环形烽火台问题、介绍Morris遍历并实现前序/中序/后序

    接着第二课的内容和带点第三课的内容. (回顾)准备一个栈,从大到小排列,具体参考上一课.... 构造数组的MaxTree [题目] 定义二叉树如下: public class Node{ public ...

  2. PAT 1043 Is It a Binary Search Tree (25分) 由前序遍历得到二叉搜索树的后序遍历

    题目 A Binary Search Tree (BST) is recursively defined as a binary tree which has the following proper ...

  3. 二叉树 遍历 先序 中序 后序 深度 广度 MD

    Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...

  4. 前序+中序->后序 中序+后序->前序

    前序+中序->后序 #include <bits/stdc++.h> using namespace std; struct node { char elem; node* l; n ...

  5. UVA 548.Tree-fgets()函数读入字符串+二叉树(中序+后序遍历还原二叉树)+DFS or BFS(二叉树路径最小值并且相同路径值叶子节点权值最小)

    Tree UVA - 548 题意就是多次读入两个序列,第一个是中序遍历的,第二个是后序遍历的.还原二叉树,然后从根节点走到叶子节点,找路径权值和最小的,如果有相同权值的就找叶子节点权值最小的. 最后 ...

  6. SDUT OJ 数据结构实验之二叉树八:(中序后序)求二叉树的深度

    数据结构实验之二叉树八:(中序后序)求二叉树的深度 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Probl ...

  7. 给出 中序&后序 序列 建树;给出 先序&中序 序列 建树

    已知 中序&后序  建立二叉树: SDUT 1489 Description  已知一棵二叉树的中序遍历和后序遍历,求二叉树的先序遍历 Input  输入数据有多组,第一行是一个整数t (t& ...

  8. SDUT-2804_数据结构实验之二叉树八:(中序后序)求二叉树的深度

    数据结构实验之二叉树八:(中序后序)求二叉树的深度 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 已知一颗二叉树的中序 ...

  9. 【C&数据结构】---关于链表结构的前序插入和后序插入

    刷LeetCode题目,需要用到链表的知识,忽然发现自己对于链表的插入已经忘得差不多了,以前总觉得理解了记住了,但是发现真的好记性不如烂笔头,每一次得学习没有总结输出,基本等于没有学习.连复盘得机会都 ...

  10. 前序 中序 后序 遍历 递归 非递归算法 java实现

    前序遍历 非递归 public void preordernorec(TreeNode root){ //System.out.println("先序遍历(非递归):"); //用 ...

随机推荐

  1. 「SOL」网络流flow (模拟赛)

    题面 给定一张分层有向图,有 \(n\) 层,每层有 \(m\) 个点.只有从第 \(i\) 层的点连向第 \(i + 1\) 层的点的连边. 记 \(A(i,j)\) 表示从第 \(i\) 层的某些 ...

  2. exp1-Password engine-加密API实现与测试

    加密API实现与测试 181210 1.准备工作 下载并查找GMT 0018-2012密码设备应用接口规范原始文档进行学习. 2.实现GMT 0018-2012接口函数 实现GMT 0018-2012 ...

  3. 嵌在Android app的html 拨打不了电话,发送不了短信

    html嵌在app里面的 <a href="tel:xxx"></a> <a href="sms:phoneNmber?body=1111& ...

  4. WPF美化常用(渐变)

    1,线性渐变色设置 2,径向渐变色设置(圆形)

  5. 简单了解如何自己动手制作RPM包

    导读 RPM文件在Linux系统中的安装最为简便.以著名的图像处理软件XV为例,其RPM包xv-3.10a-13.i386.rpm可以在该程序的主页中下载取得. 我们介绍如何自己动手制作RPM包.0, ...

  6. springboot gradle 集成流程引擎activiti

    buildscript { repositories { maven { url 'https://maven.aliyun.com/repository/gradle-plugin' } } dep ...

  7. 安装kubernetes dashboard以及用户授权

    kubernetes 版本v1.25.3 1.安装 版本查看:https://github.com/kubernetes/dashboard/releases kubectl apply -f htt ...

  8. java8 利用 ConcurrentHashMap list根据 某个属性 去重

    //自定义方法private static <T> Predicate<T> distinctByKey(Function<? super T, Object> k ...

  9. AndroidStudio中的读取本地Gradle设置,gradle-wrapper.properties内容解释

    gradle-wrapper.properties文件长成下面这个样子 distributionBase=GRADLE_USER_HOMEdistributionPath=wrapper/distsd ...

  10. 02 流程控制之while循环

    # 1.循环的语法与基本使用'''print(1)while 条件: 代码1 代码2 代码3print(3)''' # count=0# while count < 5: # 5 < 5# ...