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;
    }
  • 相关阅读:
    JavaScript 垃圾回收
    JavaScript 跳坑指南
    javaScript AJAX
    高效 JavaScript
    Java使用 Thumbnails 压缩图片
    Vue前端压缩图片
    JS input输入框字数超出长度显示省略号.....
    Vue图片浏览组件vviewer使用
    浏览器获取京东cookie
    图片在容器内水平垂直居中显示
  • 原文地址:https://www.cnblogs.com/ganxiang/p/13549894.html
Copyright © 2011-2022 走看看