How to determine whether a binary tree is complete?

前端 未结 16 1610
渐次进展
渐次进展 2021-01-02 19:15

A complete binary tree is defined as a binary tree in which every level, except possibly the deepest, is completely filled. At deepest level, all nodes must be as far left a

16条回答
  •  旧时难觅i
    2021-01-02 19:59

    bool isComplete (struct Node* root){
        if(root==NULL)
        return true;                  // Recur for left and right subtree 
        bool flag=false;
        int option1=height(root->left);
        int option2=height(root->right);
        if(option1==option2||option1==option2+1)
        flag=true;
        return flag&&isComplete(root->left)&&isComplete(root->right);
        }
    

    please consider as a correct answer if you found useful.

提交回复
热议问题