Check Completeness of a Binary Tree LeetCode solution in C++

1
class Solution {
2
public:
3
bool isComplete = true;
4

5
// Returns min, max height of node
6
pair<int, int> helper(TreeNode *root) {
7
// NULL is of height zero
8
if (root == NULL) {
9
return {0, 0};
10
}
11

12
// Moving to its children
13
pair<int, int> lst = helper(root->left);
14
pair<int, int> rst = helper(root->right);
15

16
// Height of rst is greater than lst or
17
// There are levels which are not filled other than the last level (diff
18
// between levels > 1)
19
if (rst.second > lst.first || lst.second - rst.first > 1) {
20
isComplete = false;
21
}
22

23
return {min(lst.first, rst.first) + 1, max(lst.second, rst.second) + 1};
24
}
25

26
bool isCompleteTree(TreeNode *root) {
27
helper(root);
28
return isComplete;
29
}
30
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0