Skip to content

Instantly share code, notes, and snippets.

@fpdjsns
Created December 17, 2018 11:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fpdjsns/f32430b2e112b79a5647bfdd7b7d30d7 to your computer and use it in GitHub Desktop.
Save fpdjsns/f32430b2e112b79a5647bfdd7b7d30d7 to your computer and use it in GitHub Desktop.
[leetcode] 958. Check Completeness of a Binary Tree : https://leetcode.com/problems/check-completeness-of-a-binary-tree/
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int cnt = 0;
int maxInd = 0;
void checkSubTree(TreeNode* root, int ind){
if(root == NULL) return;
cnt++;
maxInd = max(maxInd, ind);
checkSubTree(root->left, ind*2);
checkSubTree(root->right, ind*2 + 1);
}
bool isCompleteTree(TreeNode* root) {
checkSubTree(root, 1);
return cnt == maxInd;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment