当前位置:网站首页>Middle order traversal of Li Kou 94 binary tree
Middle order traversal of Li Kou 94 binary tree
2022-06-29 09:57:00 【Yingtai night snow】
Power button 94 Middle order traversal of binary trees
Title Description
Given the root node of a binary tree root , return its Middle preface Traverse . Advanced : The recursive algorithm is very simple , Can you do it by iterative algorithm ?
Input / output details

Input :root = [1,null,2,3]
Output :[1,3,2]
Input :root = []
Output :[]
Input :root = [1]
Output :[1]
Algorithm 1, Use recursion to complete the middle order , Before the order , After the sequence traversal
// Use the idea of recursion , In the sequence traversal
vector<int>inorderTraversal(TreeNode*root)
{
if(root==nullptr)
{
return res;
}
inorderTraversal(root->left);
res.push_back(root->val);
inorderTraversal(root->right);
return res;
}
// The former sequence traversal , Store the root node first and then the left node , Then the right node
vector<int>preOrderTraversal(TreeNode*root)
{
if(root==nullptr)
{
return res;
}
res.push_back(root->val);
preOrderTraversal(root->left);
preOrderTraversal(root->right);
return res;
}
// After the sequence traversal , First left, then right, and finally root
vector<int>postOrderTraversal(TreeNode*root)
{
if(root==nullptr)
{
return res;
}
postOrderTraversal(root->left);
postOrderTraversal(root->right);
res.push_back(root->val);
return res;
}
Algorithm 2, Use the stack for iterative computation
// Use iterations and stacks to implement
vector<int>inorderTraversal2(TreeNode*root)
{
vector<int>res;
// Build a stack to save the current node
stack<TreeNode*>stk;
// When a node exists , And the stack is not empty
while(root!=nullptr||!stk.empty())
{
while(root!=nullptr)
{
stk.push(root);
// When the node is not empty, the node moves to the left
root=root->left;
}
// When root When the current value is empty , Then move it to the right , And put the value in the queue
root=stk.top();
stk.pop();
res.push_back(root->val);
root=root->right;
}
return res;
}
Algorithm 3, Use Morris middle order to traverse
// Use Morris method to implement
vector<int>inorderTraversal3(TreeNode*root)
{
vector<int>res;
TreeNode*preDecessor=nullptr;
while(root!=nullptr)
{
if(root->left!=nullptr)
{
//preDecessor Node is the current root The node goes a little to the left , Then go straight to the right until you can't reach it
preDecessor=root->left;
// When preDecessor When the right node of exists and is not equal to the root node ,preDecessor Move all the way to the right
while (preDecessor->right!=nullptr&&preDecessor->right!=root)
{
preDecessor=preDecessor->right;
}
// If preDecessor Right pointer to , Continue to traverse the left subtree
if(preDecessor->right==nullptr)
{
preDecessor->right=root;
root=root->left;
}
// At this point, the left subtree has been accessed , disconnect
else{
res.push_back(root->val);
preDecessor->right=nullptr;
root=root->right;
}
}
else{
res.push_back(root->val);
root=root->right;
}
}
return res;
}
边栏推荐
- 微信小程序重写Page函数,实现全局日志记录
- 【NOI模拟赛】为NOI加点料(重链剖分,线段树)
- 官方stm32芯片包下载地址 stm32f10x stm32f40x下载
- Automatic Multi-Organ SegmVentation on Abdominal CT With Dense V-Networks
- Automatic Multi-Organ SegmVentation on Abdominal CT With Dense V-Networks
- 通用分页框架
- Making of simple addition calculator based on pyqt5 and QT Designer
- FreeRTOS(九)——队列
- Could not open JDBC connection for transaction
- Student增删gaih
猜你喜欢

C语言实现一种创建易管理易维护线程的方法

Closed training (25) basic web security

Visual assist plug-in settings for UE4 vs

zabbix4.4配置监控服务器指标,以及图形页乱码解决

Zabbix4.4 configure the indicators of the monitoring server and solve the garbled graphics pages

Deep Learning-based Automated Delineation of Head and Neck Malignant Lesions from PET Images

Gross Tumor Volume Segmentation for Head and Neck Cancer Radiotherapy using Deep Dense Multi-modalit

Segmentation of Head and Neck Tumours Using Modified U-net

Custom MVC framework implementation

A 3D Dual Path U-Net of Cancer Segmentation Based on MRI
随机推荐
Gmail:如何快速将邮件全部已读
Custom MVC framework implementation
容器
基于stm32标准库独立按键的多按键状态机的实现
你必须知道的23个最有用的Elasticseaerch检索技巧
JS获取手机型号和系统版本
How to set Google Chrome as the default browser
MySQL configuring master-slave databases
ORA-01950 对表空间无权限
LeetCode刷题——泰波那契数列
PHP内存马技术研究与查杀方法总结
基于PyQt5和Qt Designer的简易加法计算器的制作
Data governance: Metadata Management (Part 2)
Construction and use of Changan chain go language smart contract environment
指针函数和函数指针
gSoap例子——calc
XML布局中Button总是在最上层显示
Data visualization: the four quadrants of data visualization teach you to correctly apply icons
Wechat applet implements the data listener watch, including the watch that destroys the watch and sub attributes
遍历vector容器中的对象的方式