当前位置:网站首页>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;
}
};

边栏推荐
- 记录一下MySql update会锁定哪些范围的数据
- 如何管理海量的网络基础设施?
- CF566E-Restoring Map【bitset】
- Codeforces Round #392 (Div. 2) D. Ability To Convert
- [Eureka source code analysis]
- Nlp-d59-nlp game D28 - I think it's OK - stage summary - mentality adjustment
- php单例模式详解
- JCIM|药物发现中基于AI的蛋白质结构预测:影响和挑战
- el-table点击添加行样式
- 如何提高网络基础设施排障效率,告别数据断档?
猜你喜欢

生产者/消费者模型

grpc本地测试联调工具BloomRPC

About thinkphp5, use the model save() to update the data prompt method not exist:think\db\query- & gt; Error reporting solution

GeoGebra 实例 时钟

NVIDIA's CVPR 2022 oral is on fire! 2D images become realistic 3D objects in seconds! Here comes the virtual jazz band!

PTA猴子选大王(约瑟夫环问题)

Servlet快速筑基

Thinkphp5 multi language switching project practice

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

《MATLAB 神经网络43个案例分析》:第32章 小波神经网络的时间序列预测——短时交通流量预测
随机推荐
开源一款监控数据采集器,啥都能监控
Practical analysis: implementation principle of APP scanning code landing (app+ detailed logic on the web side) with source code
js代理模式
JCIM|药物发现中基于AI的蛋白质结构预测:影响和挑战
五心红娘
Thinkphp5 multi language switching project practice
indexedDB本地存储,首页优化
Endgame P.O.O
grpc本地测试联调工具BloomRPC
SSH Remote Password free login
PHP file lock
NLP-D59-nlp比赛D28—我想,也好—阶段总结—心态调整
Operator details
Detailed explanation of ThinkPHP 5.0 Model Association
桌面软件开发框架大赏
《MATLAB 神经网络43个案例分析》:第32章 小波神经网络的时间序列预测——短时交通流量预测
Summary of medical image open source datasets (II)
[GDB debugging tool] | how to debug under multithreading, multiprocessing and running programs
Grpc local test joint debugging tool bloomrpc
Honeypot 2 hfish, ehoney