leetcode 958.二叉树的完全性检验

1.题目要求:

给你一棵二叉树的根节点 root ,请你判断这棵树是否是一棵 完全二叉树 。

在一棵 完全二叉树 中,除了最后一层外,所有层都被完全填满,并且最后一层中的所有节点都尽可能靠左。最后一层(第 h 层)中可以包含 12h 个节点。

 

在这里插入图片描述

2.题目代码:

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     struct TreeNode *left;
 *     struct TreeNode *right;
 * };
 */
 //创建队列
typedef struct queue{
    struct TreeNode* data;
    struct queue* next;
}queue_t;
//入队
void push(queue_t** head,struct TreeNode* data){
    queue_t* newnode = (queue_t*)malloc(sizeof(queue_t));
    newnode->data = data;
    newnode->next = NULL;
    if(*head == NULL){
        *head = newnode;
        return;
    }
    queue_t* tail = *head;
    while(tail->next != NULL){
        tail = tail->next;
    }
    tail->next = newnode;
}
//出队
struct TreeNode* pop(queue_t** head){
    struct TreeNode* x = (*head)->data;
    (*head) = (*head)->next;
    return x; 
}
bool isCompleteTree(struct TreeNode* root) {
    int node_count = 0;//设置结点个数
    int* level_travel_node = (int*)malloc(sizeof(int) * 200);//记录最后一层的标记数组
    int j = 0;
    int depth = 0;
    queue_t* quence = NULL;
    int count = 1;
    int nextcount =  0;
    int size = 0;
    //层序遍历开始
    push(&quence,root);
    size++;
    node_count++;
    while(size != 0){
            depth++;
            for(int i = 0;i < count;i++){
                struct TreeNode* temp = pop(&quence);
                size--;
                if(temp->left != NULL){
                    push(&quence,temp->left);
                    size++;
                    node_count++;
                    nextcount++;
                }
                if(temp->right != NULL){
                    push(&quence,temp->right);
                    size++;
                    node_count++;
                    nextcount++;
                }
            }
            count = nextcount;
            nextcount = 0;
    }
    //判断结点数,如果节点数为1,则直接返回true
    if(node_count == 1){
        return true;
    }else{
        //如果节点数不为1,则判断结点总数是否符合完全二叉树的结点范围,不符合直接返回false
        if(node_count >= (int)pow(2,depth -1)&&node_count <= ((int)pow(2,depth) - 1)){
            printf("%d ",node_count);
            int depth_t = 0;
            size = 0;
            count = 1;
            int remove_line_count = 0;//记录除最后一行的节点数量
            nextcount = 0;
            push(&quence,root);
            size++;
            remove_line_count++;
            //再次进行层序遍历,一直遍历到倒数第二次位置
            while(size != 0){
                depth_t++;
                //判断是否为倒数第二层 
                if(depth_t != depth - 1){
                    for(int i = 0;i < count;i++){
                        struct TreeNode* temp = pop(&quence);
                        size--;
                        if(temp->left != NULL){
                            push(&quence,temp->left);
                            size++;
                            remove_line_count++;
                            nextcount++;
                        }
                        if(temp->right != NULL){
                            push(&quence,temp->right);
                            size++;
                            remove_line_count++;
                            nextcount++;
                        }
                }
                count = nextcount;
                nextcount = 0;
                }else{
                    //是倒数第二层后,把最后一层的结点进行记录,如果为空为-1,不为空为1
                    for(int i = 0;i < count;i++){
                        struct TreeNode* temp = pop(&quence);
                        size--;
                        if(temp->left != NULL){
                            level_travel_node[j] = 1;
                            j++;
                        }else{
                            level_travel_node[j] = -1;
                            j++;
                        }
                        if(temp->right != NULL){
                            level_travel_node[j] = 1;
                            j++;
                        }else{
                            level_travel_node[j] = -1;
                            j++;
                        }
                    }
                    break;
                }
            }
            //判断除最后一层的结点数量,如果不对,则返回false
            if(remove_line_count != ((int)pow(2,depth - 1) - 1)){
                return false;
            }else{
                int i = 0;
                for(i = 0;i < j;i++){
                    if(level_travel_node[i] == -1){
                        break;
                    }
                }
                //如果-1后的结点有1,则为错,如果不是则为true
                int j_1 = i + 1;
                for(j_1 = i + 1;j_1 < j;j_1++){
                    if(level_travel_node[j_1] == 1){
                        return false;
                    }
                }
                return true;
            }

        }else{
            return false;
        }
    }
}

解题步骤都在代码里了,虽然比较繁琐,但如果大家觉得好的话,可以给个免费的赞吗,谢谢了^ _ ^

  • 13
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值