当前位置:网站首页>408-Binary tree-preorder inorder postorder level traversal
408-Binary tree-preorder inorder postorder level traversal
2022-08-02 04:52:00 【Cat hair has almost lost cat】
存储结构
Can be sequential or chained,Basically use chains.
Sequential storage is mainly usediThe left child of the node is 2i,右孩子为2i+1.Leave it blank if you have it or not.It's okay to store a complete binary tree or a full binary tree,The storage space utilization of common trees is too low.
链式存储
typedef struct Node{
ElemType data;
Node * left; //左孩子
Node * right; //右孩子
}BTNode, *BTree;
先序中序后序层次遍历
先序:根左右
中序:左根右
后序:右根左
层次:一层一层遍历
Preorder inorder postorder recursive code
void preOrder(BTree root){
if (root != NULL)
return;
visit(root); //You can change the position of the three lines in different order.
preOrder(root->left);
preOrder(root->right);
}
Implemented using stacks and queues
//stack preorder
void preOrder(BTree root){
Stack<BTree> s;
s.push(root);
while(root != NULL || !s.empty()){
while (tmp != NULL){
tmp = tmp->left;
visit(tmp);
s.push(tmp);
}
root = s.top();
s.pop();
root = root->right;
}
}
//stack in-order
void inOrder(BTree root){
Stack<BTree> s;
s.push(root);
while(root != NULL || !s.empty()){
while (tmp != NULL){
tmp = tmp->left;
s.push(tmp);
}
root = s.top();
s.pop();
visit(root);
root = root->right;
}
}
//栈后序,来自leetcode官方.
class Solution {
public:
vector<int> postorderTraversal(TreeNode *root) {
vector<int> res;
if (root == nullptr) {
return res;
}
stack<TreeNode *> stk;
TreeNode *prev = nullptr;
while (root != nullptr || !stk.empty()) {
while (root != nullptr) {
stk.emplace(root);
root = root->left;
}
root = stk.top();
stk.pop();
if (root->right == nullptr || root->right == prev) {
res.emplace_back(root->val);
prev = root;
root = nullptr;
} else {
stk.emplace(root);
root = root->right;
}
}
return res;
}
};
//Hierarchical traversal is implemented using queues
void levelOrder(BTree root){
Queue<BTree> q;
q.push(root);
while (!q.emtpy()){
BTree tmp = q.pop();
visit(tmp);
if (tmp->left)
q.push(tmp->left);
if (tmp->right)
q.push(tmp->right);
}
}
边栏推荐
猜你喜欢
最第k大的数的一般性问题
Type c PD 电路设计
【Arduino connects SD card module to realize data reading and writing】
ICN6211:MIPI DSI转RGB视频转换芯片方案介绍 看完涨知识了呢
【Popular Science Post】UART Interface Communication Protocol
使用buildroot制作根文件系统(龙芯1B使用)
2020 - AAAI - 图像修复 Image Inpainting论文导读 -《Region Normalization for Image Inpainting》
MPU6050 accelerometer and gyroscope sensor is connected with the Arduino
分割回文串 DP+回溯 (LeetCode-131)
GM8775C MIPI转LVDS调试心得分享
随机推荐
TC358860XBG BGA65 东芝桥接芯片 HDMI转MIPI
【plang 1.4.6】Plang高级编程语言(发布)
【Arduino 连接GP2Y1014AU0F 灰尘传感器】
list:list的介绍和模拟实现
OneNET Studio与IoT Studio对比分析
【科普贴】I2C接口详解——偏硬件解析
如何快速搭建属于自己的物联网平台?
【Arduino使用旋转编码器模块】
GM8775C规格书,MIPI转LVDS,MIPI转双路LVDS分享
云服务器web项目部署详解
Mac安装MySQL详细教程
进程(下):进程控制、终止、等待、替换
D类音频功放NS4110B电路设计
MPU6050 accelerometer and gyroscope sensor is connected with the Arduino
AD PCB导出Gerber文件(非常详细的步骤)
蛮力法求解凸包问题
【plang 1.4.5】编写坦克(双人)游戏脚本
【操作系统】线程安全保护机制
Anaconda(Jupyter)里发现不能识别自己的GPU该怎么办?
AD8307对数检波器