当前位置:网站首页>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;
}
};
边栏推荐
- MoCo: Momentum Contrast for Unsupervised Visual Representation Learning
- openresty ngx_lua正则表达式
- Three.js-01 入门
- Double pointeur de liste liée (pointeur rapide et lent, pointeur séquentiel, pointeur de tête et de queue)
- The difference between MVVM and MVC
- 查看网页最后修改时间方法以及原理简介
- 使用rewrite规则实现将所有到a域名的访问rewrite到b域名
- 二叉树(二)——堆的代码实现
- 傅里叶分析概述
- H5c3 advanced - player
猜你喜欢
一文搞定JVM的内存结构
【Note17】PECI(Platform Environment Control Interface)
Arduino measures AC current
d3dx9_ What if 29.dll is missing? System missing d3dx9_ Solution of 29.dll file
a-tree 树的全部展开和收起
audiopolicy
Metaverse ape received $3.5 million in seed round financing from negentropy capital
[untitled]
PLC编程基础之数据类型、变量声明、全局变量和I/O映射(CODESYS篇 )
Un article traite de la microstructure et des instructions de la classe
随机推荐
leecode-学习笔记
Business introduction of Zhengda international futures company
Distance entre les points et les lignes
TOPSIS code part of good and bad solution distance method
openresty ngx_lua正則錶達式
Request preview display of binary data and Base64 format data
MoCo: Momentum Contrast for Unsupervised Visual Representation Learning
抖音__ac_signature
[speech processing] speech signal denoising based on Matlab GUI Hanning window fir notch filter [including Matlab source code 1711]
GWT module may need to be (RE) compiled reduce - GWT module may need to be (RE) compiled reduce
Post-90s tester: "after joining Ali, this time, I decided not to change jobs."
Arduino measures AC current
[speech processing] speech signal denoising and denoising based on MATLAB low-pass filter [including Matlab source code 1709]
Why does the C# compiler allow an explicit cast between IEnumerable&lt; T&gt; and TAlmostAnything?
513. Find the value in the lower left corner of the tree
傅里叶分析概述
航海日答题小程序之航海知识竞赛初赛
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
Methods modified by static
[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)