从上到下打印二叉树,并且用数组从左到右存储

 /*struct TreeNode {
      int val;
      struct TreeNode *left;
      struct TreeNode *right;
  };*/
int count;
struct TreeNode* Order(struct TreeNode* root,int j , int* * returnColumnSizes, int** nums){
    if(!root)
        return NULL;
    nums[j][returnColumnSizes[0][j]++] = root->val;   
    if(j+1 > count)
         count = j + 1;
    root->left = Order(root->left, j+1, returnColumnSizes, nums);
    root->right = Order(root->right, j+1,returnColumnSizes,nums);
    return root;
}
int** levelOrder(struct TreeNode* root, int* returnSize, int** returnColumnSizes){
    if(!root){
        * returnSize = 0;
        return NULL;
    }
    count = 0;
    int** nums = (int** )malloc(sizeof(int* )*1000); 
    for(int k=0; k<1000; k++){
          nums[k] = (int* )calloc(129, sizeof(int));
    }
    returnColumnSizes[0] = (int*)calloc(1000, sizeof(int));
    struct TreeNode* p = Order(root, 0, returnColumnSizes, nums);
    *returnSize = count;
    return nums;
}
上一篇:《C语言入门100例》(第33例) 矩阵转置


下一篇:【leetcode】39. 组合总和