当前位置:网站首页>LeetCode102. Sequence traversal of binary tree (output by layer and unified output)
LeetCode102. Sequence traversal of binary tree (output by layer and unified output)
2022-07-05 22:54:00 【Qingshan's green shirt】
LeetCode102. Sequence traversal of binary tree
List of articles
1. problem
2. Ideas
(1) What is hierarchy traversal
According to the number of layers, from small to large , The same layer accesses nodes from left to right
Realization :( Queue required !)
In the i If the node on the layer x At node y Left side , be x It must be y I was interviewed before . also , In the i+1 On the floor ,x The child node of must be y The child nodes of were previously accessed .
(2) With the help of queues
Operation example :
3. Code implementation
Output by layer , Just press in the above figure [[A] [BC] [DEF]] Format output .
Unified output , Namely [ABCDEF] The format of .
a. Output version by layer
class Solution {
public:
vector<vector<int>> levelOrder(TreeNode* root) {
queue<TreeNode*> que;
if (root != NULL) que.push(root);
vector<vector<int>> result;
while (!que.empty()) {
int size = que.size();
vector<int> vec;
// Be sure to use a fixed size here size, Do not use que.size(), because que.size It's changing
for (int i = 0; i < size; i++) {
//size It means traversing by sequence Because only one layer of data is left in each queue
TreeNode* node = que.front();
que.pop();
vec.push_back(node->val);
if (node->left) que.push(node->left);
if (node->right) que.push(node->right);
}
result.push_back(vec);
}
return result;
}
};
b. Unified output and publication
Just change the code above
// In this way, sequence traversal sequence can be output , But it cannot be output by layer .
class Solution {
public:
vector<vector<int>> levelOrder(TreeNode* root) {
queue<TreeNode*> que;
if (root != NULL) que.push(root);
vector<vector<int>> result;
vector<int> vec;
while (!que.empty()) {
//int size = que.size();// Just remove the hierarchy
//for (int i = 0; i < size; i++) {
TreeNode* node = que.front();
que.pop();
vec.push_back(node->val);
if (node->left) que.push(node->left);
//result.push_back(vec);
if (node->right) que.push(node->right);
//}
}
result.push_back(vec);
return result;
}
};
边栏推荐
- Editor extensions in unity
- 鏈錶之雙指針(快慢指針,先後指針,首尾指針)
- Record several frequently asked questions (202207)
- 分布式解决方案选型
- 从 1.5 开始搭建一个微服务框架——日志追踪 traceId
- Tensor attribute statistics
- Qtquick3d real time reflection
- Nail error code Encyclopedia
- Double pointeur de liste liée (pointeur rapide et lent, pointeur séquentiel, pointeur de tête et de queue)
- openresty ngx_lua正則錶達式
猜你喜欢
Finally understand what dynamic planning is
MoCo: Momentum Contrast for Unsupervised Visual Representation Learning
Distance entre les points et les lignes
[speech processing] speech signal denoising based on Matlab GUI Hanning window fir notch filter [including Matlab source code 1711]
一文搞定JVM常见工具和优化策略
Error when LabVIEW opens Ni instance finder
一文搞定class的微觀結構和指令
第十七周作业
终于搞懂什么是动态规划的
點到直線的距離直線的交點及夾角
随机推荐
[error record] file search strategy in groovy project (src/main/groovy/script.groovy needs to be used in the main function | groovy script directly uses the relative path of code)
QT creator 7-cmake update
Paddle Serving v0.9.0 重磅发布多机多卡分布式推理框架
一文搞定class的微觀結構和指令
Exponential weighted average and its deviation elimination
Shelved in TortoiseSVN- Shelve in TortoiseSVN?
Yiwen gets rid of the garbage collector
第十七周作业
點到直線的距離直線的交點及夾角
Nail error code Encyclopedia
Paddy serving v0.9.0 heavy release multi machine multi card distributed reasoning framework
Ieventsystemhandler event interface
解决thinkphp启动时“No input file specified”的问题
Navigation day answer applet: preliminary competition of navigation knowledge competition
基于STM32的ADC采样序列频谱分析
Starting from 1.5, build a micro Service Framework -- log tracking traceid
leecode-学习笔记
链表之双指针(快慢指针,先后指针,首尾指针)
我把开源项目alinesno-cloud-service关闭了
Opencv judgment points are inside and outside the polygon