当前位置:网站首页>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;
}
};
边栏推荐
- 分布式解决方案选型
- d3dx9_ How to repair 31.dll_ d3dx9_ 31. Solution to missing DLL
- VOT toolkit environment configuration and use
- 终于搞懂什么是动态规划的
- 航海日答题小程序之航海知识竞赛初赛
- 【Note17】PECI(Platform Environment Control Interface)
- Global and Chinese market of networked refrigerators 2022-2028: Research Report on technology, participants, trends, market size and share
- d3dx9_ What if 29.dll is missing? System missing d3dx9_ Solution of 29.dll file
- [groovy] mop meta object protocol and meta programming (execute groovy methods through metamethod invoke)
- Global and Chinese market of diesel fire pump 2022-2028: Research Report on technology, participants, trends, market size and share
猜你喜欢
Arduino measures AC current
Error when LabVIEW opens Ni instance finder
实现反向代理客户端IP透传
Exponential weighted average and its deviation elimination
一文搞定JVM常见工具和优化策略
查看网页最后修改时间方法以及原理简介
VOT Toolkit环境配置与使用
[screen recording] how to record in the OBS area
Ieventsystemhandler event interface
Vcomp110.dll download -vcomp110 What if DLL is lost
随机推荐
Exponential weighted average and its deviation elimination
a-tree 树的全部展开和收起
Arduino 测量交流电流
Nacos 的安装与服务的注册
Global and Chinese market of networked refrigerators 2022-2028: Research Report on technology, participants, trends, market size and share
[speech processing] speech signal denoising based on Matlab GUI Hanning window fir notch filter [including Matlab source code 1711]
H5c3 advanced - player
First, redis summarizes the installation types
Spectrum analysis of ADC sampling sequence based on stm32
抖音__ac_signature
Nail error code Encyclopedia
Openresty ngx Lua regular expression
The difference between MVVM and MVC
QT creator 7-cmake update
使用rewrite规则实现将所有到a域名的访问rewrite到b域名
Why does the C# compiler allow an explicit cast between IEnumerable&lt; T&gt; and TAlmostAnything?
Double pointer of linked list (fast and slow pointer, sequential pointer, head and tail pointer)
鏈錶之雙指針(快慢指針,先後指針,首尾指針)
Function default parameters, function placeholder parameters, function overloading and precautions
我对新中台模型的一些经验思考总结