当前位置:网站首页>Sword finger offer - print binary tree from top to bottom - (queue structure)
Sword finger offer - print binary tree from top to bottom - (queue structure)
2022-07-22 06:56:00 【zzti_ bsj】
Print binary tree from top to bottom
Print out each node of the binary tree from top to bottom , Nodes of the same layer are printed from left to right .
for example :
Given binary tree : [3,9,20,null,null,15,7],
3
/ \
9 20
/ \
15 7
return :
[3,9,20,15,7]
Tips :
Total number of nodes <= 1000
solution
/** * Definition for a binary tree node. * struct TreeNode { * int val; * struct TreeNode *left; * struct TreeNode *right; * }; */
/** * Note: The returned array must be malloced, assume caller calls free(). */
struct queue{
// 1. Location of the current element
// 2. The number of elements in the queue
int cur_elem_pos;
int last_elem_pos;
struct TreeNode *elem;
};
void init_queue(struct queue* q){
q->cur_elem_pos = 0;
q->last_elem_pos = 0;
q->elem = (struct TreeNode *)malloc(sizeof(struct TreeNode)*1010);
}
// Queue get queue head element
struct TreeNode pop(struct queue *q){
// No judgment
struct TreeNode node = q->elem[q->cur_elem_pos];
q->cur_elem_pos ++;
return node;
}
void push(struct queue * q, struct TreeNode e){
q->elem[q->last_elem_pos++] = e;
}
void printTree(struct queue *q, int *res, int *returnSize){
if(q->cur_elem_pos == q->last_elem_pos){
return;
}
// Take elements from the queue
struct TreeNode node = pop(q);
*returnSize += 1;
*res = node.val;
res++;
if(node.left != NULL){
push(q, *node.left);
}
if(node.right != NULL){
push(q, *node.right);
}
printTree(q, res, returnSize);
}
int* levelOrder(struct TreeNode* root, int* returnSize){
// Initialize queue
struct queue *q = (struct queue*)malloc(sizeof(struct queue));
init_queue(q);
// Assign a size of 1000, The type is int Space
int *res = malloc(sizeof(int)*1010);
// Initialize the length of the tree
*returnSize = 0;
if(root == NULL){
return res;
}
// push root to queue
push(q, *root);
// printTree
printTree(q, res, returnSize);
return res;
}
边栏推荐
- U++ learning note component and collision
- Lazy cancer Gospel! Software that can replace LabVIEW - atecloud Intelligent Cloud test platform
- Keithley software 2600 series 2601b | 2602b | 2604b | 2606b ns SourceMeter source table software
- [MySQL and database] MySQL & database Chapter 10: function learning
- Qt在Win10下嵌入记事本
- Device eth3 does not see to be present, delaying initialization
- EF Core 数据过滤
- 神仙测试软件藏不住了|属于中国人的“LabVIEW”你知道吗?
- Swagger interface importing postman
- Gisley keithley Software 2600 Series 2635b | 2636b | 2651a | 2657a NS sourcemeter source table Software
猜你喜欢
策略模式
Keithley software 2600 series 2635b | 2636b | 2651a | 2657a ns SourceMeter source table software
DBF菜单驱动程序:LibraryDatabaseProject
[iccv 2019] acnet: using asymmetric convolution blocks to enhance CNN's convolution kernel skeleton
【数学基础】 foundation of mathematics :Jensen不等式
QSqlDatabase: QMYSQL driver not loaded的解决方案
NI LabVIEW 2019 installation tutorial, atecloud installation free and online
【数学基础】 foundation of mathematics :拉格朗日优化和对偶
Deeply understand the principle of Redux Middleware
想要制作沙盒游戏?那么这一款插件你一定不能错过(Unity3D)
随机推荐
Gisley keithley Software 2600 Series 2635b | 2636b | 2651a | 2657a NS sourcemeter source table Software
Office 2013 customizes styles and sets styles to directories
js实现一个Promises/A+ 规范的Promise
jsp自定义标签库--foreach
echart的markArea遇到的坑啊为什么不是按照我设置的时间来显示的区间呢
When running, the object moves, rotates and scales the plug-in, "runtimetransformgizmos plug-in" tutorial (unity3d)
第三版新视野大学英语读写教程4结业考点(1,2,3,5,6单元)
logisim计组实验十 单周期MIPS CPU
容器与容器 & 容器与主机 - 通过ssh协议互联(多节点、跨主机)
自定义 Handler 时如何有效地避免内存泄漏问题?
UE4 learning notes checkbox instead of button + image
709. 转换成小写字母
Node implements batch modification of file names (file renaming)
天天爱跑步【NOIP2016 T4】
flask - { “message“: “Failed to decode JSON object: Expecting value: line 1 column 1 (char 0)“ }
吉时利Keithley软件2600系列2611B|2612B|2614B|2634B NS-SourceMeter源表软件
Foundation of Mathematics: Jensen inequality
Chrome升级到80版本遇到的问题-系统登录不了的解决方法
How to unlock HTC mobile phones officially
Deeply understand the principle of Redux Middleware