当前位置:网站首页>415-二叉树(144. 二叉树的前序遍历、145. 二叉树的后序遍历、94. 二叉树的中序遍历)
415-二叉树(144. 二叉树的前序遍历、145. 二叉树的后序遍历、94. 二叉树的中序遍历)
2022-06-24 09:33:00 【liufeng2023】
144. 二叉树的前序遍历

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);
}
public:
vector<int> preorderTraversal(TreeNode* root)
{
vector<int> res;
preorder(root, res);
return res;
}
};
class Solution
{
vector<int> res;
public:
void preorder(TreeNode* root)
{
if (root == nullptr) return;
res.push_back(root->val);
preorder(root->left);
preorder(root->right);
}
public:
vector<int> preorderTraversal(TreeNode* root)
{
preorder(root);
return res;
}
};

145. 二叉树的后序遍历

class Solution
{
vector<int> res;
public:
void preorder(TreeNode* root)
{
if (root == nullptr) return;
preorder(root->left);
preorder(root->right);
res.push_back(root->val);
}
public:
vector<int> postorderTraversal(TreeNode* root)
{
preorder(root);
return res;
}
};

94. 二叉树的中序遍历

class Solution
{
vector<int> res;
public:
void preorder(TreeNode* root)
{
if (root == nullptr) return;
preorder(root->left);
res.push_back(root->val);
preorder(root->right);
}
public:
vector<int> inorderTraversal(TreeNode* root)
{
preorder(root);
return res;
}
};

边栏推荐
猜你喜欢

Idea cannot save settings source root d:xxxx is duplicated in module XXX

英伟达这篇CVPR 2022 Oral火了!2D图像秒变逼真3D物体!虚拟爵士乐队来了!

Analysis of 43 cases of MATLAB neural network: Chapter 32 time series prediction of wavelet neural network - short-term traffic flow prediction

GeoGebra 实例 时钟

SSH Remote Password free login

Conseils étonnants pour promouvoir les ventes d'entreprise avec le chat en direct

tp5 使用post接收数组数据时报variable type error: array错误的解决方法

如何管理海量的网络基础设施?

e的lnx为什么等于x

ggplot2颜色设置总结
随机推荐
PHP file lock
port 22: Connection refused
vim的使用
带文字的seekbar : 自定义progressDrawable/thumb :解决显示不全
canvas 绘制图片
impdp导schema报ORA-31625异常处理
Amendment to VPP implementation policy routing
PHP封装一个文件上传类(支持单文件多文件上传)
使用Live Chat促进业务销售的惊人技巧
Dragging El table sortablejs
观察者模式
An open source monitoring data collector that can monitor everything
How to solve multi-channel customer communication problems in independent stations? This cross-border e-commerce plug-in must be known!
Servlet fast foundation building
数组无缝滚动demo
Five heart matchmaker
When should gridfs be used?
如何规范化数据中心基础设施管理流程
Oracle database listening file configuration
Operator details