当前位置:网站首页>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;
}
边栏推荐
- Kicad learning notes - shortcut keys
- 在Activity外使用startActivity()方法报错原因与解决办法
- 长安链GO语言智能合约环境搭建及使用
- 367. effective complete square dichotomy
- Could not open JDBC connection for transaction
- Data governance: data standard management (Part III)
- InvalidConnectionAttributeException异常处理
- c语言printf大家族系列
- General part: cognition, design and best practice of prototype design
- Invalidconnectionattributeexception exception exception handling
猜你喜欢

Chang'an chain go language smart contract writing and compilation

装饰器模式的应用,包装ServletRequest,增加addParameter方法

kdevelop新建工程

基于PyQt5和Qt Designer的简易加法计算器的制作
![[Huawei certification] the most complete and selected question bank in hcia-datacom history (with answer analysis)](/img/d4/f5ea847573433f7ca7bd429f57e40a.png)
[Huawei certification] the most complete and selected question bank in hcia-datacom history (with answer analysis)

A 3D Dual Path U-Net of Cancer Segmentation Based on MRI

linux环境下安装配置redis,并设置开机自启动

Lc236. nearest common ancestor of binary tree

Mysql配置主从数据库

KDevelop new project
随机推荐
linux下centos7中mysql5.7安装教程
【NOI模拟赛】为NOI加点料(重链剖分,线段树)
我想知道如何免费网上注册股票开户?另外,手机开户安全么?
C语言中通过sprintf()函数构造sql语句
F5 BIG-IP iControl REST命令执行(CVE-2022-1388)
IDEA调试失败,报JDWP No transports initialized, jvmtiError=AGENT_ERROR_TRANSPORT_LOAD(196)
数据源连接池未关闭的问题 Could not open JDBC Connection for transaction
Fully Automated Delineation of Gross Tumor Volume for Head and Neck Cancer on PET-CT Using Deep Lear
zabbix4.4配置监控服务器指标,以及图形页乱码解决
gSoap例子——calc
How to set Google Chrome as the default browser
監控數據源連接池使用情况
Is it safe to open an account for stock speculation? Is it reliable?
Lc236. nearest common ancestor of binary tree
[technology development] development and design of alcohol tester solution
Cloud management platform: openstack architecture design and detailed interpretation
linux环境下安装配置redis,并设置开机自启动
云管理平台:9大开源云管理平台(CMP)
Es error nonodeavailableexception[none of the configured nodes are available:[.127.0.0.1}{127.0.0.1:9300]
Generic paging framework