其他分享
首页 > 其他分享> > 从上到下打印二叉树,并且用数组从左到右存储

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

作者:互联网

 /*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;
}

标签:TreeNode,struct,nums,int,从左到右,二叉树,returnColumnSizes,root,从上到下
来源: https://blog.csdn.net/qq_52001969/article/details/113822604