当前位置:网站首页>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;
}
};
边栏推荐
- Postman core function analysis - parameterization and test report
- Usage Summary of scriptable object in unity
- Golang writes the opening chapter of selenium framework
- 30 optimization skills about mysql, super practical
- 记录几个常见问题(202207)
- 媒体查询:引入资源
- Three.js-01 入门
- 【无标题】
- Three.JS VR看房
- d3dx9_ How to repair 31.dll_ d3dx9_ 31. Solution to missing DLL
猜你喜欢
![[groovy] mop meta object protocol and meta programming (Introduction to groovyobject interface | introduction to metaclass | implementation of class methods using groovyobject invokemethod)](/img/48/cd7960bbbc51a967b20da410bf81fe.jpg)
[groovy] mop meta object protocol and meta programming (Introduction to groovyobject interface | introduction to metaclass | implementation of class methods using groovyobject invokemethod)

EasyCVR集群部署如何解决项目中的海量视频接入与大并发需求?

d3dx9_ How to repair 31.dll_ d3dx9_ 31. Solution to missing DLL

VOT toolkit environment configuration and use

audiopolicy

Arduino measures AC current

513. Find the value in the lower left corner of the tree

一文搞定class的微觀結構和指令

Spectrum analysis of ADC sampling sequence based on stm32

Qtquick3d real time reflection
随机推荐
实现反向代理客户端IP透传
Request preview display of binary data and Base64 format data
分布式解决方案选型
Record several frequently asked questions (202207)
Hcip day 11 (BGP agreement)
Vision Transformer (ViT)
[secretly kill little buddy pytorch20 days] - [Day2] - [example of picture data modeling process]
Metasploit (MSF) uses MS17_ 010 (eternal blue) encoding:: undefined conversionerror problem
GWT module may need to be (RE) compiled reduce - GWT module may need to be (RE) compiled reduce
一文搞定class的微觀結構和指令
Leetcode weekly The 280 game of the week is still difficult for the special game of the week's beauty team ~ simple simulation + hash parity count + sorting simulation traversal
从 1.5 开始搭建一个微服务框架——日志追踪 traceId
Distance from point to line intersection and included angle of line
Un article traite de la microstructure et des instructions de la classe
Leetcode daily question 1189 The maximum number of "balloons" simple simulation questions~
Paddy serving v0.9.0 heavy release multi machine multi card distributed reasoning framework
My experience and summary of the new Zhongtai model
Binary tree (II) -- code implementation of heap
如何快速理解复杂业务,系统思考问题?
航海日答题小程序之航海知识竞赛初赛