当前位置:网站首页>Morris traversal of binary tree
Morris traversal of binary tree
2022-06-10 19:16:00 【Starlight technician】
Binary tree Morris Traverse

If you can modify the original tree, you can use morris Traverse , because morris Traversal using the leaf node null pointer to achieve ;
A node with a left tree can be reached twice , Determine the number of times to return to the node according to the direction of the rightmost node of the left subtree ,
1. Morris Ergodic order
#include<iostream>
#include<vector>
using namespace std;
class TreeNode
{
public:
TreeNode* left;
TreeNode* right;
int val;
TreeNode(int v){
val = v;left = nullptr;right = nullptr;}
TreeNode(){
val = 0; left = nullptr; right = nullptr;}
};
void Morris(TreeNode* root)
{
TreeNode* cur = root;
TreeNode* mostnode = nullptr;
while (cur != nullptr)
{
mostnode = cur->left;
if (mostnode != nullptr)
{
while (mostnode != nullptr && mostnode->right != cur)
{
mostnode = mostnode->right;
}
// For the first time cur
if (mostnode->right == nullptr)
{
cout<<cur->val<<" ";
mostnode->right = cur;
cur = cur->left;
continue;
}
// Come to... For the second time cur
else
{
cout<<cur->val<<" ";
mostnode->right = nullptr;
}
}
else
cur = cur->right;
}
}
moris The first sequence traversal
morris Traverse , Each node with a left subtree will arrive twice , It is specified that only when a node is reached for the first time , To achieve traversal ;code
#include<iostream>
#include<vector>
using namespace std;
class TreeNode
{
public:
TreeNode* left;
TreeNode* right;
int val;
TreeNode(int v){
val = v;left = nullptr;right = nullptr;}
TreeNode(){
val = 0; left = nullptr; right = nullptr;}
};
void Morris(TreeNode* root)
{
TreeNode* cur = root;
TreeNode* mostnode = nullptr;
while (cur != nullptr)
{
mostnode = cur->left;
if (mostnode != nullptr)
{
while (mostnode != nullptr && mostnode->right != cur)
{
mostnode = mostnode->right;
}
// For the first time cur
if (mostnode->right == nullptr)
{
cout << cur->val << " "; // The first sequence traversal
mostnode->right = cur;
cur = cur->left;
continue;
}
// Come to... For the second time cur
else
{
mostnode->right = nullptr;
}
}
else
cur = cur->right;
}
}
2.Morris In the sequence traversal
Thought analysis
Only once , Print directly ; Reach the node twice , Second arrival printcode
#include<iostream>
#include<vector>
using namespace std;
class TreeNode
{
public:
TreeNode* left;
TreeNode* right;
int val;
TreeNode(int v){
val = v;left = nullptr;right = nullptr;}
TreeNode(){
val = 0; left = nullptr; right = nullptr;}
};
void Morris(TreeNode* root)
{
TreeNode* cur = root;
TreeNode* mostnode = nullptr;
while (cur != nullptr)
{
mostnode = cur->left;
if (mostnode != nullptr)
{
while (mostnode != nullptr && mostnode->right != cur)
{
mostnode = mostnode->right;
}
// For the first time cur
if (mostnode->right == nullptr)
{
mostnode->right = cur;
cur = cur->left;
continue;
}
// Come to... For the second time cur
else
{
mostnode->right = nullptr;
}
}
cout << cur->val << " "; // In the sequence traversal
cur = cur->right;
}
}
3.Morris After the sequence traversal
Ideas
When a node arrives twice , Print the bounded tree of the left subtree of the node in reverse order ; Finally, print the right boundary of the whole tree in reverse order ; When printing the right boundary of the tree in reverse order , Need to reverse the right boundary right Pointer pointing , Restore after printingcode
#include<iostream>
#include<vector>
using namespace std;
class TreeNode
{
public:
TreeNode* left;
TreeNode* right;
int val;
TreeNode(int v){
val = v;left = nullptr;right = nullptr;}
TreeNode(){
val = 0; left = nullptr; right = nullptr;}
};
TreeNode* reverse_Tree(TreeNode* node)
{
TreeNode* pre = nullptr;
TreeNode* cur = node;
while (cur != nullptr)
{
TreeNode* temp = cur->right;
cur->right = pre;
pre = cur;
cur = temp;
}
return pre;
}
void printEdge(TreeNode* root)
{
TreeNode* node= reverse_Tree(root);
TreeNode* cur = node;
while (cur!= nullptr)
{
cout << cur->val << " ";
cur = cur->right;
}
TreeNode* temp = reverse_Tree(node);
}
void Morris(TreeNode* root)
{
TreeNode* cur = root;
TreeNode* mostnode = nullptr;
while (cur != nullptr)
{
mostnode = cur->left;
if (mostnode != nullptr)
{
while (mostnode != nullptr && mostnode->right != cur)
{
mostnode = mostnode->right;
}
// For the first time cur
if (mostnode->right == nullptr)
{
mostnode->right = cur;
cur = cur->left;
continue;
}
// Come to... For the second time cur
else
{
mostnode->right = nullptr;
printEdge(cur->left);
}
}
cur = cur->right;
}
printEdge(root);
return;
}
4. Morris Traversal judgment search binary tree
When traversing a binary tree , Node values are always in ascending order , So the tree in this lesson is the search binary tree
Edge ergodic , Edge judgment
- code
#include<iostream>
#include<vector>
using namespace std;
class TreeNode
{
public:
TreeNode* left;
TreeNode* right;
int val;
TreeNode(int v){
val = v;left = nullptr;right = nullptr;}
TreeNode(){
val = 0; left = nullptr; right = nullptr;}
};
bool is_BST(TreeNode* root)
{
TreeNode* cur = root;
TreeNode* mostnode = nullptr;
int preValue = INT_MIN;
while (cur != nullptr)
{
mostnode = cur->left;
if (mostnode != nullptr)
{
while (mostnode != nullptr && mostnode->right != cur)
{
mostnode = mostnode->right;
}
// For the first time cur
if (mostnode->right == nullptr)
{
mostnode->right = cur;
cur = cur->left;
continue;
}
// Come to... For the second time cur
else
{
mostnode->right = nullptr;
}
}
if(cur->val < prevalue)
return false;
prevalue = cur->val;
cur = cur->right;
}
return true;
}
5. Regression traversal and Morris When to use traversal
When we must realize the strong integration of binary tree to complete the problem , Recursion must be used ; For example, you must get information from the left subtree of a node , Then take the information from the right subtree , Then we can get the result ; This means that a node has reached three times , This problem must be traversed recursively ;
边栏推荐
- 第四章 数据类型(三)
- 基于谱加权的波束方向图分析
- Tencent cloud database tdsql- a big guy talks about the past, present and future of basic software
- How to play the Dragon Boat Festival "immersive cloud Tour"? That is to say, it helps "live broadcast +" new scene landing
- Chapter IV data type (III)
- [database language SPL] a simple and fast database language SPL
- 5. golang generics and reflection
- Cross domain error: when allowcredentials is true, allowedorigins cannot contain the special value "*“
- 数据库防火墙技术展望【终章】
- 端午“沉浸式云旅游”怎么玩?即构助力“直播+”新场景落地
猜你喜欢

北京地铁票务系统

Adobe Premiere Foundation (track related) (V)

源码分析及实践测试OpenFeign负载均衡

数据库防火墙闪亮登场(好文共赏)

Tencent cloud database tdsql- a big guy talks about the past, present and future of basic software

Adobe Premiere基础特效(卡点和转场)(四)

数据治理经典6大痛点?这本书教你解决

腾讯云数据库TDSQL-大咖论道 | 基础软件的过去、现在、未来

Mysql (17 déclencheurs)

2022.05.26(LC_1143_最长公共子序列)
随机推荐
Wireshark learning notes (I) common function cases and skills
直播预告 | 社交新纪元,共探元宇宙社交新体验
【 random talk 】 congratulations on getting the title of CSDN expert. Your efforts will eventually pay off
nodejs-判断系统类型-获取主机名称-执行控制台命令-中文乱码
3. getting started with golang concurrency
2022.05.28(LC_516_最长回文子序列)
2022.05.25(LC_718_最长重复子数组)
Rewrite clear Bayesian formula with base ratio
[vulnhub range] janchow: 1.0.1
Mysql8.0 (summary of new features)
APICloud可视化开发丨一键生成专业级源码
Chapter IV data type (III)
VIM common shortcut keys
Win32-子窗口-父窗口-窗口所有者
Ruixin micro rk1126 platform platform porting libevent cross compiling libevent
2022.05.29(LC_6079_价格减免)
Beijing Metro ticketing system
【数据库语言SPL】写着简单跑得又快的数据库语言 SPL
MySQL (17 trigger)
瑞芯微RK1126平台 平台移植libevent 交叉编译libevent