博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
leetcode——Binary Tree Level Order Traversal
阅读量:4137 次
发布时间:2019-05-25

本文共 2860 字,大约阅读时间需要 9 分钟。

树的遍历主要是四种:前序、中序遍历、后序、分层遍历。前三种一般用递归的方法,而递归方法都会用到堆栈;分层遍历需要用到队列,队列的实现也是不唯一的,根据需要建立自己的结构。这道题具体的方法是,先将根入栈,记录size=1(我的size在队列中),出栈生成需要的数组,size = 0;再将他的左右儿子入栈,size...一直循环到size=0,也就是不再有入栈且完全出栈的时候(描述不好。。)。

这道题对题目的理解很重要:leetcode中 形参为(整形)指针,一般是要传回的整形;形参为二维指针,要传回数组,而前一个整形正是这个数组的大小。

/*** Return an array of arrays of size *returnSize. 返回二维数组的大小* The sizes of the arrays are returned as *columnSizes array.   *columnSizes是一个数组,注意,不是columnSizes* Note: Both returned array and *columnSizes array must be malloced, assume caller calls free().*/

这道题参考了别人的答案,但能做出来很开心啊,加油!

* Definition for a binary tree node.* struct TreeNode {*     int val;*     struct TreeNode *left;*     struct TreeNode *right;* };*//*** Return an array of arrays of size *returnSize.* The sizes of the arrays are returned as *columnSizes array.* Note: Both returned array and *columnSizes array must be malloced, assume caller calls free().*/#include
#include
#define ElementType struct TreeNodetypedef struct QueueRecord *Queue;typedef struct TreeNode *Tree;struct TreeNode { int val; struct TreeNode *left; struct TreeNode *right;};//创建一个队列--链表实现struct QueueRecord{ int size; ElementType *element; Queue next;};//省略空间检测Queue Initialize(){ Queue H = (Queue)malloc(sizeof(struct QueueRecord)); H->size = 0; H->element = NULL; H->next = NULL; return H;}void Enqueue(ElementType *node, Queue Q){ Queue temp; temp = (Queue)malloc(sizeof(struct QueueRecord)); Q->size++; temp->element = node; temp->next = Q->next; Q->next = temp;}Queue Dequeue(Queue Q){ Queue temp = Q; Queue tem; if (temp->next) { while (temp->next->next != NULL) { temp = temp->next; } } tem = temp->next; temp->next = NULL; Q->size--; return tem;}//*returnSize 返回数组大小,*columnSizes 是返回数组内部数组的大小的数组int** levelOrder(struct TreeNode* root, int** columnSizes, int* returnSize) { if (root == NULL) return NULL; Queue Q = Initialize(); Enqueue(root, Q); int lever = 4; int **Ret = (int**)malloc(lever * sizeof(int*)); int size; int *column; int *colsize = (int*)malloc(lever*sizeof(int)); Queue out; int j = 0; while (Q->size != 0) { size = Q->size; int i = 0; column = (int*)malloc(size*sizeof(int)); colsize[j] = size; do { out = Dequeue(Q); column[i++] = out->element->val; if (out->element->left) { Enqueue(out->element->left,Q); } if (out->element->right) { Enqueue(out->element->right, Q); } } while (--size); if (j >= lever-1) { lever = lever << 1; Ret = (int**)realloc(Ret, lever*sizeof(int*));//重新分配地址空间 colsize = (int*)realloc(colsize, lever*sizeof(int*)); } Ret[j++] = column; } *columnSizes = colsize; *returnSize = j; return Ret;}void main(){ Tree T = (Tree)malloc(1 * sizeof(struct TreeNode)); T->val = 1; T->left = NULL; T->right = NULL; int **c = (int**)malloc(1 * sizeof(int*));; int *r = (int *)malloc(sizeof(int)); int ** ret = levelOrder(T, c, r); getchar();}

转载地址:http://jhovi.baihongyu.com/

你可能感兴趣的文章
Unifrax宣布新建SiFAB™生产线
查看>>
艾默生纪念谷轮™在空调和制冷领域的百年创新成就
查看>>
NEXO代币持有者获得20,428,359.89美元股息
查看>>
Piper Sandler为EverArc收购Perimeter Solutions提供咨询服务
查看>>
RMRK筹集600万美元,用于在Polkadot上建立先进的NFT系统标准
查看>>
JavaSE_day14 集合中的Map集合_键值映射关系
查看>>
异常 Java学习Day_15
查看>>
Mysql初始化的命令
查看>>
MySQL关键字的些许问题
查看>>
浅谈HTML
查看>>
css基础
查看>>
Servlet进阶和JSP基础
查看>>
servlet中的cookie和session
查看>>
过滤器及JSP九大隐式对象
查看>>
软件(项目)的分层
查看>>
菜单树
查看>>
Servlet的生命周期
查看>>
JAVA八大经典书籍,你看过几本?
查看>>
《读书笔记》—–书单推荐
查看>>
JAVA数据类型
查看>>