当前位置:网站首页>Preorder, inorder, follow-up of binary tree (non recursive version)
Preorder, inorder, follow-up of binary tree (non recursive version)
2022-07-01 15:38:00 【Try to be better zz】
Tips : When the article is finished , Directories can be generated automatically , How to generate it, please refer to the help document on the right
List of articles
Preface
The pre order, middle order and post order of the non recursive version of the binary tree are also often tested in the interview , Must be proficient in !
Next, we will talk about how to print the first, middle and last sequence of binary tree non recursively .
One 、 Before the order
Ideas : Data structure stack is generally used to change recursion to non recursion , Print when the current node is out of the stack , And a right node is added to the right node first ,
Then add the left node , Because the antecedent root is about , The stack is in reverse order , So first enter the node .
void Print(TreeNode* root)
{
// Before the order
stack<TreeNode*> s = stack<TreeNode*>();// Create a stack
s.push(root);// Head node stack
The order of stacking is right pressing right , There is left pressure left
while (!s.empty())
{
TreeNode* cur = s.top();
s.pop();
cout << to_string(cur->val)+"" ;
if (cur->right)
{
s.push(cur->right);
}
if (cur->left)
{
s.push(cur->left);
}
}
}
Two 、 In the following order
Ideas : Different from the previous sequence , In the following sequence, we need two stacks to exchange , The result order of the first stack is root right left , Because the stack is in reverse order , We will s1 Put the data in the stack s2 You get left and right , Subsequent operations are similar to those in the previous sequence .
In the following order
stack<TreeNode*> s1 = stack<TreeNode*>();
stack<TreeNode*> s2 = stack<TreeNode*>();
s1.push(root);
while (!s1.empty())
{
TreeNode* cur = s1.top();
s1.pop();
s2.push(cur);
if (cur->left)
{
s1.push(cur->left);
}
if (cur->right)
{
s1.push(cur->right);
}
}
while (!s2.empty())
{
TreeNode* cur = s2.top();
s2.pop();
cout << cur->val << endl;
}
3、 ... and 、 Middle preface
The code is as follows :
Middle preface
stack<TreeNode*> t = stack<TreeNode*>();
TreeNode* cur = root;
while (!t.empty() || cur!=nullptr)
{
if (cur!= nullptr)
{
t.push(cur);
cur = cur->left;
}
else
{
cur = t.top();
t.pop();
cout << cur->val << endl;
cur = cur->right;
}
}
summary
边栏推荐
- 【目标跟踪】|STARK
- Phpcms background upload picture button cannot be clicked
- SAP s/4hana: one code line, many choices
- Go zero actual combat demo (I)
- k8s部署redis哨兵的实现
- Pico,能否拯救消费级VR?
- 厦门灌口镇田头村特色农产品 甜头村特色农产品蚂蚁新村7.1答案
- STM32F4-TFT-SPI时序逻辑分析仪调试记录
- Implementation of deploying redis sentry in k8s
- Flink 系例 之 TableAPI & SQL 与 MYSQL 分组统计
猜你喜欢

微信小程序03-文字一左一右显示,行内块元素居中

Zhang Chi Consulting: lead lithium battery into six sigma consulting to reduce battery capacity attenuation
Sort out the four commonly used sorting functions in SQL

Skywalking 6.4 distributed link tracking usage notes

Introduction to MySQL audit plug-in

【300+精选大厂面试题持续分享】大数据运维尖刀面试题专栏(三)

Reading notes of top performance version 2 (V) -- file system monitoring

厦门灌口镇田头村特色农产品 甜头村特色农产品蚂蚁新村7.1答案

Summary of point cloud reconstruction methods I (pcl-cgal)
Implementation of deploying redis sentry in k8s
随机推荐
常见健身器材EN ISO 20957认证标准有哪些
MySQL backup and restore single database and single table
The last picture is seamlessly connected with the first picture in the swiper rotation picture
Photoshop plug-in HDR (II) - script development PS plug-in
Gaussdb (for MySQL):partial result cache, which accelerates the operator by caching intermediate results
Junda technology - wechat cloud monitoring scheme for multiple precision air conditioners
Summary of week 22-06-26
【300+精选大厂面试题持续分享】大数据运维尖刀面试题专栏(三)
Introduction to MySQL audit plug-in
Qt+pcl Chapter 9 point cloud reconstruction Series 2
Basic use process of cmake
ThinkPHP进阶
Wechat applet 02 - Implementation of rotation map and picture click jump
微信小程序03-文字一左一右显示,行内块元素居中
软件测试的可持续发展,必须要学会敲代码?
远程办公经验?来一场自问自答的介绍吧~ | 社区征文
雷神科技冲刺北交所,拟募集资金5.4亿元
[target tracking] | template update time context information (updatenet) "learning the model update for Siamese trackers"
STM32ADC模拟/数字转换详解
How to realize clock signal frequency division?