当前位置:网站首页>Traversal of binary tree and related knowledge
Traversal of binary tree and related knowledge
2022-06-23 07:04:00 【Peanut butter noodles】
List of articles
1. Recursive traversal
1.1 The former sequence traversal
vector<int> res;
vector<int> PreTravelsal(vector<int> res, TreeNode* root)
{
res.clear();
travelsal(root);
return res;
}
void travelsal(TreeNode* cur)
{
if(cur == NULL) return;
res.push_back(cur->val);
travelsal(cur->left);
travelsal(cur->right);
}
1.2 In the sequence traversal
vector<int> res;
vector<int> PreTravelsal(vector<int> res, TreeNode* root)
{
res.clear();
travelsal(root);
return res;
}
void travelsal(TreeNode* cur)
{
if(cur == NULL) return;
res.push_back(cur->val);
//travelsal(cur->left);
res.push_back(cur->val);
travelsal(cur->right);
}
1.3 After the sequence traversal
vector<int> res;
vector<int> PreTravelsal(vector<int> res, TreeNode* root)
{
res.clear();
travelsal(root);
return res;
}
void travelsal(TreeNode* cur)
{
if(cur == NULL) return;
//res.push_back(cur->val);
travelsal(cur->left);
travelsal(cur->right);
res.push_back(cur->val);
}
2. Iterate through
2.1 The former sequence traversal
vector<int> travelsal(TreeNode* root)
{
vector<int> res;
stack<TreeNode*> st;
if(root == NULL) return res;
st.push(root);
while(!st.empty())
{
TreeNode* cur = st.top();
st.pop();
res.push_back(cur->val);
if(cur->right) st.push(cur->right);
if(cur->left) st.push(cur->left);
}
return res;
}
2.2 After the sequence traversal
vector<int> travelsal(TreeNode* root)
{
vector<int> res;
stack<TreeNode*> st;
if(root == NULL) return res;
st.push(root);
while(!st.empty())
{
TreeNode* cur = st.top();
st.pop();
res.push_back(cur->val);
if(cur->left) st.push(cur->left);
if(cur->right) st.push(cur->right);
}
reverse(res.begin(), res.end());
return res;
}
2.3 In the sequence traversal
vector<int> travelsal(TreeNode* root)
{
vector<int> res;
stack<TreeNode*> st;
TreeNode* cur = root;
while(cur != NULL || !st.empty())
{
if(cur != NULL)
{
st.push(cur);
cur = cur->left;
}
else
{
cur = st.top();
st.pop();
res.push_back(cur->val);
cur = cur->right;
}
}
return res;
3. Sequence traversal
vector<int> travelsal(TreeNode* root)
{
vector<int> res;
queue<TreeNode*> que;
if(root == NULL) return res;
que.push(root);
while(!que.empty())
{
int n = que.size();
for(int i = 0; i < n; 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);
}
}
return res;
}
4. The species of binary trees
- Perfect binary tree : A binary tree has only a degree of 0 The node and degree of are 2 The node of , And the degree is 0 On the same layer , Its depth is k, The number of nodes is 2^k-1;

- Perfect binary tree : In a complete binary tree , Except that the lowest node may not be full , The number of nodes in each layer reaches the maximum , And the nodes of the lowest layer are concentrated in the leftmost positions of the layer . If the bottom layer is No h layer , Then the layer contains 1~ 2^(h-1) Nodes .

- Binary search tree : If its left subtree is not empty , Then the value of all nodes in the left subtree is less than the value of its root node ; If its right subtree is not empty , Then the value of all nodes in the right subtree is greater than the value of its root node ; Its left 、 The right subtree is also a binary sort tree .

4. Balanced binary search trees : Also known as AVL(Adelson-Velsky and Landis) Trees , And has the following properties : It is an empty tree or the absolute value of the height difference between its left and right subtrees does not exceed 1, And both the left and right subtrees are a balanced binary tree .
边栏推荐
- QT designer cannot modify the window size, and cannot change the size by dragging the window with the mouse
- Idea automatically generates serialVersionUID
- [graduation season · advanced technology Er] it's my choice. I have to walk on my knees
- /bin/sh no such file or directory问题
- 900. RLE 迭代器
- 318. 最大单词长度乘积
- swagger3整合oauth2 认证token
- Badly placed ()‘s 问题
- Storage mode of data in memory (C language)
- [saison de remise des diplômes · technologie agressive er] votre choix, agenouillez - vous et partez
猜你喜欢
![[STL] summary of map usage of associated containers](/img/1d/1b6488ea47face0548500b1e1ec60d.png)
[STL] summary of map usage of associated containers

cmder

QT designer cannot modify the window size, and cannot change the size by dragging the window with the mouse

MySQL的意向共享锁、意向排它锁和死锁

直播回顾 | 传统应用进行容器化改造,如何既快又稳?

MySQL redo log redo log

Easy EDA learning notes 09 esp32-wroom-32e module esp32-devkitc-v4 development board one click download circuit

Swagger3 integrates oauth2 authentication token

MySQL mvcc multi version concurrency control

网页制作存在的一些难点
随机推荐
如何在 PHP 中进行日期格式验证检查(正则)
Anti chicken soup speech
idea安装 CloudToolkit 插件
[STL] summary of deque usage of sequential containers
js 判断两个数组增加和减少的元素
图解三次握手四次挥手,小白都能看懂
Wechat applet - Global Monitoring of certain attribute changes of GlobalData, such as monitoring of network state switching
开源OAuth2框架 实现SSO单点登录
Solve the mining virus sshd2 (redis does not set a password and clear the crontab scheduled task)
产品-Axure9(英文版),原型设计后台动态二级菜单显示内容
[STL] unordered of associated container_ Map Usage Summary
数据统计与分析基础 实验一 基本语法及运算
C # how to obtain DPI and real resolution (can solve the problem that has been 96)
2121. sum of intervals of the same elements - hash table method
What are the pension financial products in 2022? Low risk
/bin/sh no such file or directory问题
Swagger3 integrates oauth2 authentication token
300. 最长递增子序列
313. 超级丑数
[STL] summary of stack and queue usage of container adapter