当前位置:网站首页>先序遍历,中序遍历,后序遍历,层序遍历
先序遍历,中序遍历,后序遍历,层序遍历
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;
}
};
边栏推荐
- 如何操作数据库
- 默默重新开始,第一页也是新的一页
- 报错:flask: TypeError: ‘function‘ object is not iterable
- APP商品详情源数据接口(淘宝/京东/拼多多/苏宁/抖音等平台详情数据分析接口)代码对接教程
- Import the pycharm environment package into another environment
- AD的library中 库文件后缀有.intlib .schlib .pcblib 的区别
- bzoj 5333 [Sdoi2018]荣誉称号
- Quectel EC20 4G module dial related
- 推进产教融合 赋能教育创新发展 | 华云数据荣获“企业贡献奖”
- 数据库中间件-jdbi
猜你喜欢
6 states of a thread
工控设备的系统如何进行加固
IQ Products巨细胞病毒CMV感染检测试剂盒的特征和应用
Go lang1.18入门精炼教程——第一章:环境搭建
Quectel EC20 4G module dial related
IQ Products CMV Brite Turbo试剂盒的原理
Error jinja2.exceptions.UndefinedError: 'form' is undefined
Data center project preliminary summary
运放-运算放大器经典应用电路大全-应用电路大全
GNNExplainer applied to node classification task
随机推荐
golang xml 处理动态属性
mysql 总结
使用百度EasyDL实现智能垃圾箱
Flask failed to create database without error
db.sqlite3 has no "as Data Source" workaround
什么是excel文件保护
Qt learning (3) - Qt module
Unity backgammon game design and simple AI implementation (1)
思维方法 解决问题的能力
普罗米修斯原理及节点发布
GNNExplainer applied to node classification task
Search 1688 product interface by image (item_search_img-search 1688 product by image (Politao interface) code docking tutorial
Use baidu EasyDL intelligent bin
uniapp实现防抖搜索
The Integer thread safe
如何操作数据库
抗菌药物丨Toronto Research Chemicals 天冬酰胺D
电学知识的疑问
安装flask
像天才一样思考:如何培养自己的创造力?