首页 > 其他分享 >102. 二叉树的层序遍历C

102. 二叉树的层序遍历C

时间:2024-03-20 11:37:00浏览次数:16  
标签:struct returnSize int 层序 tail 二叉树 102 array size

/**
 * 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 *returnColumnSizes array.
 * Note: Both returned array and *columnSizes array must be malloced, assume caller calls free().
 */
int** levelOrder(struct TreeNode* root, int* returnSize, int** returnColumnSizes) {
    *returnSize=0;
    if(!root) return NULL;
    struct TreeNode* queue[2500];
    int head=0,tail=0,size=1;
    queue[0]=root;
    int* column=(int*)malloc(sizeof(int)*2500);
    int** array=(int**)malloc(sizeof(int*)*2500);
    while(size!=0){
        column[*returnSize]=size;
        array[*returnSize]=(int*)malloc(sizeof(int)*size);
        for(int i=head;i<=tail;i++) array[*returnSize][i-head]=queue[i]->val;
        (*returnSize)++;
        int start=head,end=tail;
        printf("%d%d ",start,end);
        for(;start<=end;start++){
            struct TreeNode* temp=queue[start];
            head++;
            size--;
            if(temp->left){
                queue[++tail]=temp->left;
                size++;
            }
            if(temp->right){
                queue[++tail]=temp->right;
                size++;
            }
        }
        printf("%d %d",size,tail);
    }    
    *returnColumnSizes=column;
    return array;
}

 

标签:struct,returnSize,int,层序,tail,二叉树,102,array,size
From: https://www.cnblogs.com/llllmz/p/18084854

相关文章

  • 代码随想录算法训练营第十五天| 226. 翻转二叉树 101. 对称二叉树
    226.翻转二叉树https://leetcode.cn/problems/invert-binary-tree/description/publicTreeNodeinvertTree(TreeNoderoot){invert(root);returnroot;}publicvoidinvert(TreeNodenode){if(node==null)return;TreeNod......
  • 洛谷题单指南-二叉树-P1185 绘制二叉树
    原题链接:https://www.luogu.com.cn/problem/P1185题意解读:在表格中绘制二叉树,有几个关键点1、结点用小写字母o 表示,对于一个父亲结点,用 / 连接左子树,用 \连接右子树,表格其余地方填空格。2、第m层结点若两个属于同一个父亲,那么它们之间由 3个空格隔开;若两个结点相邻但......
  • 第七节:二叉树的先序、中序、后续遍历的多种递归写法
    一.        二.        三.         !作       者:Yaopengfei(姚鹏飞)博客地址:http://www.cnblogs.com/yaopengfei/声     明1:如有错误,欢迎讨论,请勿谩骂^_^。声     明2:原创博客请在转载......
  • 代码随想录算法训练营第十四天| 二叉树相关
    二叉树的递归遍历递归三要素:确定递归函数的参数和返回值,确定终止条件,确定单层递归的逻辑144.二叉树的前序遍历https://leetcode.cn/problems/binary-tree-preorder-traversal/description/publicList<Integer>preorderTraversal(TreeNoderoot){List<Integer>......
  • 【算法与数据结构】二叉树链式结构的实现【前中后序】
    文章目录......
  • 对称二叉树
    这里考试的时候其实就是想考递归,但是我实在是不清楚为什么\(n\)能够开到\(100W\)。。。这不随便超时吗介绍一个确定性算法的判断一个二叉树是否对称首先一个二叉树的中序遍历有两种,一个是先遍历左子树,一个是先遍历右子树,我们用结构归纳法,可以证明以树根为中心翻转其中一种中序遍......
  • 数据结构入门——二叉树(中)
    通过《二叉树(上)》的学习,我们已经对二叉树有了基本的了解,那我们现在继续深入了解二叉树。二叉树的存储结构顺序存储顺序结构存储就是使用数组来存储,一般使用数组只适合表示完全二叉树,因为不是完全二叉树会有空间的浪费。而现实中使用中只有堆才会使用数组来存储,关于堆我们后......
  • 二叉树|二叉树理论基础、二叉树的递归遍历
    代码随想录(programmercarl.com)树和二叉树1.树的基本概念1.1树的定义1.2树的逻辑表示方法1.3树的基本术语1.4树的性质1.5树的基本运算1.6树的存储结构2.二叉树的概念和性质2.1二叉树的定义2.2二叉树的性质2.3二叉树与树、森林之间的转换3.二叉树的存储结构3.1......
  • Unity类银河恶魔城学习记录10-14 p102 Applying damage to skills and clean up源代码
     Alex教程每一P的教程原代码加上我自己的理解初步理解写的注释,可供学习Alex教程的人参考此代码仅为较上一P有所改变的代码【Unity教程】从0编程制作类银河恶魔城游戏_哔哩哔哩_bilibiliEntity.csusingSystem.Collections;usingSystem.Collections.Generic;usingUnit......
  • 洛谷题单指南-二叉树-P3884 [JLOI2009] 二叉树问题
    原题链接:https://www.luogu.com.cn/problem/P3884题意解读:要计算二叉树的深度、宽度、节点间的距离,深度、宽度的概念很好理解,节点间的距离描述是:节点u,v之间的距离表示从u到v的最短有向路径上向根节点的边数的两倍加上向叶节点的边数。说人话就是:u到v的距离=uv最近公共祖先到u......