classSolution{public:boolisCompleteTree(TreeNode*root){constintcount=getCount(root);returnvalidIndex(root,1,count);}private:// Calculate the # of nodesintgetCount(TreeNode*root){if(root==nullptr)return0;return1+getCount(root->left)+getCount(root->right);}// Make sure no index is > the # of nodesboolvalidIndex(TreeNode*root,intindex,intcount){if(root==nullptr)returntrue;if(index>count)returnfalse;returnvalidIndex(root->left,index*2,count)&&validIndex(root->right,index*2+1,count);}};