How to determine whether a binary tree is complete?

前端 未结 16 1677
渐次进展
渐次进展 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条回答
  •  孤独总比滥情好
    2021-01-02 19:40

    For a tree to be complete

    height(left) == height(right) or height(left) == 1+height(right)

        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);
        }
    

提交回复
热议问题