当前位置:网站首页>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 ;
边栏推荐
- c(指针02)
- nodejs-判断系统类型-获取主机名称-执行控制台命令-中文乱码
- Detailed explanation of Lora module wireless transceiver communication technology
- Adobe Premiere基础-导入导出,合并素材,源文件编译,脱机(二)
- [database language SPL] a simple and fast database language SPL
- libcurl 7.61.0 VS2013 编译教程
- Libcurl 7.61.0 vs2013 compilation tutorial
- Leecode27977 double finger needling
- Adobe Premiere基础(视频的最后一步字幕添加)(六)
- C (pointer-02)
猜你喜欢

WordPress 6.0 "Arturo Arturo" release

APICloud可视化开发丨一键生成专业级源码

What are the current mainstream all-optical technology solutions- Part II

Adobe Premiere foundation - material nesting (animation of Tiktok ending avatar) (IX)

基于谱加权的波束方向图分析

Cross domain error: when allowcredentials is true, allowedorigins cannot contain the special value "*“

Adobe Premiere basic special effects (card point and transition) (IV)

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

Three ways generated by stream lambda

Design and implementation of online ordering system based on SSM Rar (project source code)
随机推荐
Chapter 6 relational data theory exercise
Openssl1.1.1 VS2013-编译教程
Array type of DB2 SQL pl
2022.05.29(LC_6079_价格减免)
Adobe Premiere Foundation (the last step of video subtitle adding) (6)
第四章 数据类型(三)
[database language SPL] a simple and fast database language SPL
[Code] neural symbol generation machine
Google Earth engine (GEE) -- Copernicus atmosphere monitoring (CAMs) global aerosol AOI near real-time observation data set
VS从txt文件读取中文汉字产生乱码的解决办法(超简单)
Uncertainty reasoning: let the model know that it doesn't know
[Agency] 10 minutes to master the essential difference between forward agency and reverse agency
Debugging skills
第三章 数据类型(二)
SQL function
【 random talk 】 congratulations on getting the title of CSDN expert. Your efforts will eventually pay off
VIM common shortcut keys
What are the current mainstream all-optical technology solutions- Part II
5. golang generics and reflection
SQL 函数