当前位置:网站首页>Recursive traversal of 414 binary tree
Recursive traversal of 414 binary tree
2022-06-24 10:02:00 【liufeng2023】
1、 Binary tree recursive traversal
Three elements of recursive algorithm :
- Determine the parameters and return values of recursive functions : Determine which parameters need to be processed in the recursive process , Then add this parameter to the recursive function , What is the return value of each recursion, and then determine the return type of recursive function .
- Determine termination conditions : The recursive algorithm is finished , When it's running , We often encounter stack overflow errors , The termination condition is not written or the termination condition is not written correctly , The operating system also uses a stack structure to store the recursive information of each layer , If recursion doesn't stop , The memory stack of the operating system is bound to overflow .
- Determine termination conditions : The recursive algorithm is finished , When it's running , We often encounter stack overflow errors , The termination condition is not written or the termination condition is not written correctly , The operating system also uses a stack structure to store the recursive information of each layer , If recursion doesn't stop , The memory stack of the operating system is bound to overflow .
The former sequence traversal :
class Solution {
public:
void traversal(TreeNode* cur, vector<int>& vec) {
if (cur == NULL) return;
vec.push_back(cur->val); // in
traversal(cur->left, vec); // Left
traversal(cur->right, vec); // Right
}
vector<int> preorderTraversal(TreeNode* root) {
vector<int> result;
traversal(root, result);
return result;
}
};
In the sequence traversal :
void traversal(TreeNode* cur, vector<int>& vec) {
if (cur == NULL) return;
traversal(cur->left, vec); // Left
vec.push_back(cur->val); // in
traversal(cur->right, vec); // Right
}
After the sequence traversal :
void traversal(TreeNode* cur, vector<int>& vec) {
if (cur == NULL) return;
traversal(cur->left, vec); // Left
traversal(cur->right, vec); // Right
vec.push_back(cur->val); // in
}
边栏推荐
猜你喜欢

Nvisual digital infrastructure operation management software platform

使用Live Chat促進業務銷售的驚人技巧

Thinkphp5 multi language switching project practice

《MATLAB 神经网络43个案例分析》:第32章 小波神经网络的时间序列预测——短时交通流量预测

如何让社交媒体成为跨境电商驱动力?这款独立站工具不能错过!

How to make social media the driving force of cross-border e-commerce? This independent station tool cannot be missed!

Summary of medical image open source datasets (II)

二叉樹第一部分

js单例模式

How does home office manage the data center network infrastructure?
随机推荐
Oracle database listening file configuration
Prct-1400: failed to execute getcrshome resolution
GIS实战应用案例100篇(十四)-ArcGIS属性连接和使用Excel的问题
LeetCode: 377. Combined sum IV
CICFlowMeter源码分析以及为满足需求而进行的修改
Grpc local test joint debugging tool bloomrpc
队列Queue
PHP encapsulates a file upload class (supports single file and multiple file uploads)
SSH Remote Password free login
el-table表格的拖拽 sortablejs
414-二叉树的递归遍历
如何管理海量的网络基础设施?
Thinkphp5 multi language switching project practice
Codeforces Round #392 (Div. 2) D. Ability To Convert
Practical analysis: implementation principle of APP scanning code landing (app+ detailed logic on the web side) with source code
js单例模式
Oracle viewing data file header SCN information
【Eureka 源码分析】
Use of vim
算法--找到和最大的长度为 K 的子序列(Kotlin)