这段代码,在后面跑测试用例时,出现了stack-overflow,但是原因还不清楚。

问题如下:

 二叉树的层次遍历
 

给定一个二叉树,返回其按层次遍历的节点值。 (即逐层地,从左到右访问所有节点)。

例如:
给定二叉树: [3,9,20,null,null,15,7],

    3
/ \
9 20
/ \
15 7

返回其层次遍历结果:

[
[3],
[9,20],
[15,7]
]

因为要输出一个二维数组,那么我需要定义一个两重链表,具体定义如下:
/* 链表节点 用于存储输出结果 */
struct listNode {
int val;
struct listNode *next;
}; struct list {
int count;
struct listNode *head;
struct listNode *tail;
struct list *next;
}; struct list_list
{
int count;
struct list *head;
struct list *tail;
};

链表的一些操作如下:

void init_list(struct list *l)
{
l->count = 0;
l->head = NULL;
l->tail = NULL;
l->next = NULL;
} void init_list_list(struct list_list *l)
{
l->count = 0;
l->head = NULL;
l->tail = NULL;
} void add_new_node(struct list *l, struct listNode *node)
{
if (!l->head)
{
l->head = node;
l->tail = node;
l->count = 1;
return;
} l->tail->next = node;
l->tail = node;
l->count++;
} void add_new_list(struct list_list *ll, struct list *l)
{
if (ll->head == NULL)
{
ll->head = l;
ll->tail = l;
ll->count = 1;
return;
} ll->tail->next = l;
ll->tail = l;
ll->count++;
}

链表创建如下:

struct list_list * create_list_list()
{
struct list_list *ll = malloc(sizeof(struct list_list));
if (ll)
{
init_list_list(ll);
}
return ll;
}
struct list * create_list()
{
struct list *l = malloc(sizeof(struct list));
if (l)
{
init_list(l);
} return l;
}

另外需要定义一个队列,用于存放每个树节点

/* 队列节点,用于存储已经遍历过的根节点 */
struct queue_node
{
void *entry;
struct queue_node *next;
}; struct queue {
struct queue_node *front;
struct queue_node *rear;
};

队列的一些简单操作实现如下:

/* 队列节点,用于存储已经遍历过的根节点 */
struct queue_node
{
void *entry;
struct queue_node *next;
}; struct queue {
struct queue_node *front;
struct queue_node *rear;
}; void init_queue(struct queue *q)
{
q->front = NULL;
q->rear = NULL;
} void queue_push(struct queue *q, void *np)
{
struct queue_node *node = malloc(sizeof(struct queue_node));
node->entry = np;
node->next = NULL; if (q->rear == NULL)
{
q->rear = node;
q->front = node;
}
else
{
q->rear->next = node;
q->rear = node;
}
} void *queue_pop(struct queue *q)
{
struct queue_node *np = q->front;
void *entry = NULL;
if (np)
{
entry = np->entry;
if (np->next == NULL)
q->rear = NULL;
q->front = np->next;
free(np);
} return entry;
}
struct queue * create_queue()
{
struct queue *q = malloc(sizeof(struct queue));
if (q)
{
init_queue(q);
}
return q;
}

主函数的具体实现思想为,遍历根节点,将其存入队列中,然后在队列中插入一个flag标记,之后进入一个while循环,循环中,每次从队列中取出一个成员,将该成员存入到用于输出的二层链表中,然后判断其左右孩子是否为空,不为空,则其左右孩子入队列。然后再循环,当从队列中取出的是flag标志后,并且队列中还没空,那么就在这个队列中再插入一个flag标志,表示这一层的遍历结束,可以输出这一层的链表。如此循环,直到所有节点都入队列,读到最后一个flag标记,并且队列为空,那么整个遍历流程结束。后面就是把二层链表输出成一个二维数组。

代码如下:

int** levelOrder(struct TreeNode* root, int* returnSize, int** returnColumnSizes){
struct list_list *ll;
struct list *subList;
struct listNode *head;
struct queue *q;
struct TreeNode *pNode = NULL;
struct listNode *newnode = NULL;
int **r;
int *subr;
struct list *l;
int i,j;
int *size;
int *resize; if (!root || !returnSize || !returnColumnSizes)
return; q = create_queue();
ll = create_list_list();
l = create_list();
add_new_list(ll, l);
queue_push(q, (void*)root);
queue_push(q, FINISH_FALG); while (q->front != NULL)
{
pNode = (struct TreeNode *)queue_pop(q);
if (pNode == FINISH_FALG)
{
if (q->front == NULL)
break;
/* 创建新的链表,在总链表中插入新的链表 */
l = create_list();
add_new_list(ll, l); /* 在当前队列中再插入一个终结标志 */
queue_push(q, FINISH_FALG);
continue;
} /* 该节点插入到当前链表中 */
newnode = create_node(pNode->val);
add_new_node(l, newnode); /* 将当前节点的左右孩子加入队列中 */
if (pNode->left != NULL)
{
queue_push(q, (void*)pNode->left);
}
if (pNode->right != NULL)
{
queue_push(q, (void*)pNode->right);
}
} r = (int **)malloc(sizeof(int *) * ll->count);
resize = (int *)malloc(sizeof(int *) * ll->count);
subList = ll->head;
while(subList && i < ll->count)
{
subr = (int *)malloc(sizeof(int) * subList->count);
head = subList->head;
j = 0;
while(head && j < subList->count)
{
subr[j] = head->val;
j++;
head = head->next;
} resize[i] = subList->count; r[i] = subr;
i++;
subList = subList->next;
}
*returnSize = ll->count;
*returnColumnSizes = resize;
return r;
}

(leetcode)二叉树的层次遍历-c语言实现的更多相关文章

  1. LeetCode 二叉树的层次遍历

    第102题 给定一个二叉树,返回其按层次遍历的节点值. (即逐层地,从左到右访问所有节点). 例如: 给定二叉树: [3,9,20,null,null,15,7], 3 / \ 9 20 / \ 15 ...

  2. LeetCode 二叉树的层次遍历 C++

    给定一个二叉树,返回其按层次遍历的节点值. (即逐层地,从左到右访问所有节点). 例如:给定二叉树: [3,9,20,null,null,15,7], 3 / \ 9 20 / \ 15 7 返回其层 ...

  3. (leetcode)二叉树的前序遍历-c语言实现

    给定一个二叉树,返回它的 前序 遍历. 示例: 输入: [1,null,2,3] 1 \ 2 / 3 输出: [1,2,3] 进阶: 递归算法很简单,你可以通过迭代算法完成吗? 前序遍历 前序遍历首先 ...

  4. LeetCode 107 ——二叉树的层次遍历 II

    1. 题目 2. 解答 与 LeetCode 102 --二叉树的层次遍历 类似,我们只需要将每一层的数据倒序输出即可. 定义一个存放树中数据的向量 data,一个存放树的每一层数据的向量 level ...

  5. LeetCode:二叉树的层次遍历||【107】

    LeetCode:二叉树的层次遍历||[107] 题目描述 给定一个二叉树,返回其节点值自底向上的层次遍历. (即按从叶子节点所在层到根节点所在的层,逐层从左向右遍历) 例如:给定二叉树 [3,9,2 ...

  6. LeetCode 102. 二叉树的层次遍历(Binary Tree Level Order Traversal) 8

    102. 二叉树的层次遍历 102. Binary Tree Level Order Traversal 题目描述 给定一个二叉树,返回其按层次遍历的节点值. (即逐层地,从左到右访问所有节点). 每 ...

  7. LeetCode 102. Binary Tree Level Order Traversal 二叉树的层次遍历 C++

    Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...

  8. Leetcode 102 二叉树的层次遍历 Python

    二叉树的层次遍历 给定一个二叉树,返回其按层次遍历的节点值. (即逐层地,从左到右访问所有节点). 例如: 给定二叉树: [3,9,20,null,null,15,7],   3   / \ 9 20 ...

  9. Leetcode题目102.二叉树的层次遍历(队列-中等)

    题目描述: 给定一个二叉树,返回其按层次遍历的节点值. (即逐层地,从左到右访问所有节点). 例如: 给定二叉树: [3,9,20,null,null,15,7], 3 / \ 9 20 / \ 15 ...

随机推荐

  1. Dubbo基础二之架构及处理流程概述

    Dubbo基础一之实战初体验 - 池塘里洗澡的鸭子 - 博客园 (cnblogs.com)中体验了Dubbo的使用,对于消费端对服务提供者的调用非常清晰明确.那么Dubbo是如何做到的呢?下面对Dub ...

  2. Understanding JSON Schema

    json schema 在线校验器 译自:Understanding JSON Schema { "type": "object", "propert ...

  3. tor-browse

    https://sourceforge.net/projects/t-browser/

  4. 国内商业智能软件的天下来了,选择BI工具我们应该看这3点!

    ​数据变革时代,经常有"外行人"问到,何谓商业智能BI? BI可以说是通过软件或者服务,将网络时代中海量的数据转化为行动上的洞察力,从而影响企业的战略和战术决策. 当海量.高增长率 ...

  5. 如何利用Smartbi做数据分析:2018内5月热销乘用车分析报告

    在2018年第一季度热销乘用车分析报告中,SUV以总体销量15.4%的同比增长率让人不可小觑,Smartbi刚得到5月分析的数据就迫不及待的来看看是否热度不减,结果在5月这个所谓汽车销售淡季,轿车以9 ...

  6. .net core多环境发布部署

    1.新建的asp.net core项目默认会有appsettings.json和appsettings.Development.json, 新建一个生产环境appsettings.Production ...

  7. Qt:QCustomPlot使用教程(二)——基本绘图

    0.说明 本节翻译总结自:Qt Plotting Widget QCustomPlot - Basic Plotting 本节内容是使用QCustomPlot进行基本绘图. 本节教程都使用custom ...

  8. Excel:获取等差时间

    假设:从0:01:05开始,每隔1分30秒生成一个时间项 做法: 在A2处写 =TIME( 0,1,5 )构建一个TIME类型0:01:05,如果要构建别的时间,就按照TIME( 时 , 分 , 秒  ...

  9. 微信小程序结合laravel完成签到功能

    前端样式未做处理,可将后端数据传至前端进行处理 1.wxml页面 <!--pages/signIn/signIn.wxml--> <view class='signIn'> & ...

  10. JVM探究 面试题 JVM的位置 三种JVM:HotSpot 新生区 Young/ New 养老区 Old 永久区 Perm 堆内存调优GC的算法有哪些?标记清除法,标记压缩,复制算法,引用计数法

    JVM探究 面试题: 请你弹弹你对JVM的理解?Java8虚拟机和之前的变化更新? 什么是OOM?什么是栈溢出StackOverFlowError?怎么分析 JVM的常用调优参数有哪些? 内存快照如何 ...