当前位置:网站首页>先序遍历,中序遍历,后序遍历,层序遍历
先序遍历,中序遍历,后序遍历,层序遍历
2022-08-09 06:34:00 【未央吖】
先序遍历
递归:
class Solution {
public:
void preorder(TreeNode *root, vector<int> &res) {
if (root == nullptr) {
return;
}
res.push_back(root->val);
preorder(root->left, res);
preorder(root->right, res);
}
vector<int> preorderTraversal(TreeNode *root) {
vector<int> res;
preorder(root, res);
return res;
}
};
迭代:栈
class Solution {
public:
vector<int> preorderTraversal(TreeNode* root) {
stack<TreeNode*> st;
vector<int> ret;
if (root) st.push(root);
while (!st.empty()) {
TreeNode*node = st.top();
st.pop();
ret.push_back(node->val); //中
if (node->right) st.push(node->right); //右
if (node->left) st.push(node->left); //左
}return ret;
}
};中序遍历
递归:
class Solution {
public:
void inorder(TreeNode* root, vector<int>& res) {
if (!root) {
return;
}
inorder(root->left, res); //左
res.push_back(root->val); //中
inorder(root->right, res); //右
}
vector<int> inorderTraversal(TreeNode* root) {
vector<int> res;
inorder(root, res);
return res;
}
};
迭代:栈
class Solution {
public:
vector<int> inorderTraversal(TreeNode* root) {
stack<TreeNode*>st;
TreeNode*cur = root;
vector<int> ret;
while (cur || !st.empty()) {
while (cur) { //先遍历完所有左子树
st.push(cur);
cur = cur->left;
}
TreeNode*res = st.top();
st.pop();
ret.push_back(res->val); //中
cur = res->right; //右
}
return ret;
}
};后序遍历
递归:
class Solution {
public:
void postorder(TreeNode *root, vector<int> &res) {
if (root == nullptr) {
return;
}
postorder(root->left, res); //左
postorder(root->right, res); //右
res.push_back(root->val); //中
}
vector<int> postorderTraversal(TreeNode *root) {
vector<int> res;
postorder(root, res);
return res;
}
};
迭代:栈
class Solution {
public:
vector<int> postorderTraversal(TreeNode* root) {
stack<TreeNode*>st;
vector<int> ret;
if (root) st.push(root);
while (!st.empty()) {
TreeNode*cur = st.top();
ret.push_back(cur->val); //中
st.pop();
if (cur->left) {st.push(cur->left);} //左
if (cur->right) {st.push(cur->right);} //右
}
reverse(ret.begin(), ret.end()); //反转
return ret;
}
};层序遍历
class Solution {
public:
vector<vector<int>> levelOrder(TreeNode* root) {
vector<vector<int>> ret;
if (!root) return ret;
queue<TreeNode*> que;
que.push(root);
while (!que.empty()) {
int size = que.size();
vector<int> res;
for (int i = 0; i < size; i++) {
TreeNode*cur = que.front();
que.pop();
res.push_back(cur->val);
if (cur->left) {que.push(cur->left);}
if (cur->right) {que.push(cur->right);}
}
ret.push_back(res);
}
return ret;
}
};边栏推荐
- 字节跳动面试题之镜像二叉树2020
- 详解C语言中的wait()函数和waitpid()函数
- Unity backgammon game design and simple AI implementation (1)
- AD的library中 库文件后缀有.intlib .schlib .pcblib 的区别
- 电学知识的疑问
- Altium designer software commonly used the most complete package library, including schematic library, PCB library and 3D model library
- C# 利用iTextSharp画PDF
- Service
- Flask failed to create database without error
- 使用百度EasyDL实现智能垃圾箱
猜你喜欢
随机推荐
变压器的工作原理(图解,原理图讲解,一看就懂)
The Integer thread safe
缓存技术使用
APP product source data interface (taobao, jingdong/spelling/suning/trill platform details a lot data analysis interface) code and docking tutorial
flask创建数据库失败未报错
String.toLowerCase(Locale.ROOT)
[MySQL] Second, the relationship between processes, MySQL password cracking, table building and database building related commands
Unity C# 委托——事件,Action,Func的作用和区别
jvm线程状态
为什么以太网无法接收大于1500字节的数据包?
jdepend
Unity backgammon game design and simple AI implementation (1)
Teach you how to make the Tanabata meteor shower in C language - elegant and timeless (detailed tutorial)
Explain the wait() function and waitpid() function in C language in detail
cut命令的使用实例
运放-运算放大器经典应用电路大全-应用电路大全
mmdetection源码解析--ResNet18
Word文件的只读模式没有密码怎么退出?
Error: flask: TypeError: 'function' object is not iterable
按图搜索1688商品接口(item_search_img-按图搜索1688商品(拍立淘接口)代码对接教程









