当前位置:网站首页>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;
}
};
边栏推荐
- Global and Chinese market of networked refrigerators 2022-2028: Research Report on technology, participants, trends, market size and share
- 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
- Shelved in TortoiseSVN- Shelve in TortoiseSVN?
- 南京:全面启用商品房买卖电子合同
- Methods modified by static
- Nangou Gili hard Kai font TTF Download with installation tutorial
- VIM tail head intercept file import
- 媒体查询:引入资源
- a-tree 树的全部展开和收起
- Nacos installation and service registration
猜你喜欢
第十七周作业
Navigation day answer applet: preliminary competition of navigation knowledge competition
点到直线的距离直线的交点及夹角
One article deals with the microstructure and instructions of class
d3dx9_ How to repair 31.dll_ d3dx9_ 31. Solution to missing DLL
Unity Max and min constraint adjustment
BFC block level formatting context
我把开源项目alinesno-cloud-service关闭了
Distance entre les points et les lignes
Post-90s tester: "after joining Ali, this time, I decided not to change jobs."
随机推荐
Postman核心功能解析-参数化和测试报告
509. Fibonacci Number. Sol
Codeforces Global Round 19
Usage Summary of scriptable object in unity
傅里叶分析概述
Assign the output of a command to a variable [repeat] - assigning the output of a command to a variable [duplicate]
Unity Max and min constraint adjustment
一文搞定垃圾回收器
2022.02.13 - SX10-30. Home raiding II
利用LNMP实现wordpress站点搭建
Masked Autoencoders Are Scalable Vision Learners (MAE)
How to create a thread
Opencv judgment points are inside and outside the polygon
Nail error code Encyclopedia
thinkphp5.1跨域问题解决
Paddle Serving v0.9.0 重磅发布多机多卡分布式推理框架
Binary tree (III) -- heap sort optimization, top k problem
Metasploit (MSF) uses MS17_ 010 (eternal blue) encoding:: undefined conversionerror problem
Leetcode daily question 1189 The maximum number of "balloons" simple simulation questions~
Golang writes the opening chapter of selenium framework