Created
December 17, 2018 11:50
-
-
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/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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