zoukankan      html  css  js  c++  java
  • 【leetcode】打印二叉树链表

    int count = 0;
    void func(int** arr,int* hash,struct TreeNode* node,int row)
    {
        row++;
        if (hash[row] == 0) //如果该行列数为0 那证明是当前遍历到的是该行第一个数(空值已经排除)
        {
            count++; 
            int* row_arr = (int*)calloc(1000,sizeof(int)); 
            arr[row] = row_arr;
            row_arr[hash[row]++] = node->val; //赋值完后列数+1                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           
        }
        else if(hash[row] > 0)
        {
            arr[row][hash[row]++] = node->val;
        }
        if (node->left != NULL)
        {
            func(arr,hash,node->left,row);
        }
        if (node->right != NULL)
        {
            func(arr,hash,node->right,row);
        }
    }
    int** levelOrder(struct TreeNode* root, int* returnSize, int** returnColumnSizes){
        int** arr = (int**)calloc(10000,sizeof(int));
        int* hash = (int*)calloc(10000,sizeof(int)); //用来存放每行的列数
        if (root == NULL)
        {
            *returnSize = NULL;
            return NULL;
        }
        int* row_arr = (int*)calloc(1,sizeof(int));    
        int row = 0; //行数
        count = 0; //定义了全局变量 用来最后返回行数
        row_arr[hash[row]++] = root->val;
        arr[row] = row_arr;    
        count++;    
        if (root->left != NULL)
        {
            func(arr,hash,root->left,row);
        }
        if (root->right != NULL)
        {
            func(arr,hash,root->right,row);
        }
        *returnSize = count;  //行数
        *returnColumnSizes = hash; //每行列数指针
        return arr;
    }
  • 相关阅读:
    Java学习之路
    ofo开锁共享平台
    Texstudio
    我的母亲 (老舍)
    Excel数据透视表
    Excel分类汇总与数据有效性
    Tomcat源码分析
    证明:在任意六人的聚会中,要么有三人曾经认识,要么有三人不曾认识
    琅琊榜读书笔记
    选择排序可视化
  • 原文地址:https://www.cnblogs.com/ganxiang/p/13549894.html
Copyright © 2011-2022 走看看