当前位置:网站首页>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;
}
};
边栏推荐
- 3 find the greatest common divisor and the least common multiple
- 一文搞定JVM的内存结构
- Global and Chinese markets of industrial pH meters 2022-2028: Research Report on technology, participants, trends, market size and share
- New 3D particle function in QT 6.3
- Spectrum analysis of ADC sampling sequence based on stm32
- The code generator has deoptimised the styling of xx/typescript. js as it exceeds the max of 500kb
- 解决thinkphp启动时“No input file specified”的问题
- 一文搞定垃圾回收器
- audiopolicy
- Binary tree (II) -- code implementation of heap
猜你喜欢

Metaverse ape ape community was invited to attend the 2022 Guangdong Hong Kong Macao Great Bay metauniverse and Web3.0 theme summit to share the evolution of ape community civilization from technology

Error when LabVIEW opens Ni instance finder

一文搞定垃圾回收器

Starting from 1.5, build a micro Service Framework -- log tracking traceid

Tensor attribute statistics

谷歌地图案例

Postman core function analysis - parameterization and test report

Vision Transformer (ViT)

Un article traite de la microstructure et des instructions de la classe
![[error record] groovy function parameter dynamic type error (guess: groovy.lang.missingmethodexception: no signature of method)](/img/3e/34b45cd14f0302bb381efd244bc68f.jpg)
[error record] groovy function parameter dynamic type error (guess: groovy.lang.missingmethodexception: no signature of method)
随机推荐
Assign the output of a command to a variable [repeat] - assigning the output of a command to a variable [duplicate]
QT creator 7 beta release
openresty ngx_lua正則錶達式
Solve the problem of "no input file specified" when ThinkPHP starts
Boring boring
audiopolicy
从 1.5 开始搭建一个微服务框架——日志追踪 traceId
openresty ngx_lua请求响应
使用rewrite规则实现将所有到a域名的访问rewrite到b域名
Arduino measures AC current
Metaverse ape ape community was invited to attend the 2022 Guangdong Hong Kong Macao Great Bay metauniverse and Web3.0 theme summit to share the evolution of ape community civilization from technology
audiopolicy
终于搞懂什么是动态规划的
如何创建线程
30 optimization skills about mysql, super practical
Hcip day 11 (BGP agreement)
Global and Chinese market of water treatment technology 2022-2028: Research Report on technology, participants, trends, market size and share
Distributed resource management and task scheduling framework yarn
Exponential weighted average and its deviation elimination
如何快速理解复杂业务,系统思考问题?