题解 | #判断是不是完全二叉树#

判断是不是完全二叉树

https://www.nowcoder.com/practice/8daa4dff9e36409abba2adbe413d6fae

/**
 * struct TreeNode {
 *	int val;
 *	struct TreeNode *left;
 *	struct TreeNode *right;
 * };
 */
/**
 * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
 *
 * 
 * @param root TreeNode类 
 * @return bool布尔型
 */
bool isCompleteTree(struct TreeNode* root ) {
    // write code here
    if(root == NULL) return true;
    struct TreeNode** queue = (struct TreeNode**)malloc(sizeof(struct TreeNode*) * 100);
    int qIdx = -1;
    int hIdx = -1;
    queue[++qIdx] = root;
    bool empty = false;
    while(qIdx != hIdx) {
        struct TreeNode* c = queue[++hIdx];
        if(c != NULL) queue[++qIdx] = c->left;
        if(c != NULL) queue[++qIdx] = c->right;
        if(c == NULL) empty = true;
        if(empty == true && c != NULL) {
            free(queue);
            return false;
        }
    }
    free(queue);
    return true;
}

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务