Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 4540b3c

Browse files
committed
Add CheckCompletenessOfABinaryTree problem.
1 parent 84d83bf commit 4540b3c

File tree

15 files changed

+140
-0
lines changed

15 files changed

+140
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* struct TreeNode {
4+
* int val;
5+
* TreeNode *left;
6+
* TreeNode *right;
7+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
8+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
9+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
10+
* };
11+
*
12+
* std::ostream& operator<< (std::ostream& os, TreeNode node);
13+
*/
14+
class Solution {
15+
public:
16+
bool isCompleteTree(TreeNode* root) {
17+
}
18+
};
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
### Approach
2+
3+
4+
### Implementation
5+
6+
```cpp
7+
class Solution {
8+
public:
9+
bool isCompleteTree(TreeNode* root) {
10+
deque<TreeNode*> stack;
11+
stack.push_back(root);
12+
13+
bool foundNull = false;
14+
while (!stack.empty()) {
15+
TreeNode* node = stack.front();
16+
stack.pop_front();
17+
18+
if (node == nullptr) {
19+
foundNull = true;
20+
continue;
21+
} else {
22+
if (foundNull == true) {
23+
return false;
24+
} else {
25+
stack.push_back(node->left);
26+
stack.push_back(node->right);
27+
}
28+
}
29+
}
30+
return true;
31+
}
32+
};
33+
```
34+
35+
### Complexity Analysis
36+
* Time complexity :
37+
38+
* Space complexity :
39+
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* struct TreeNode {
4+
* int val;
5+
* TreeNode *left;
6+
* TreeNode *right;
7+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
8+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
9+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
10+
* };
11+
*
12+
* std::ostream& operator<< (std::ostream& os, TreeNode node);
13+
*/
14+
class Solution {
15+
public:
16+
bool isCompleteTree(TreeNode* root) {
17+
deque<TreeNode*> stack;
18+
stack.push_back(root);
19+
20+
bool foundNull = false;
21+
while (!stack.empty()) {
22+
TreeNode* node = stack.front();
23+
stack.pop_front();
24+
25+
if (node == nullptr) {
26+
foundNull = true;
27+
continue;
28+
} else {
29+
if (foundNull == true) {
30+
return false;
31+
} else {
32+
stack.push_back(node->left);
33+
stack.push_back(node->right);
34+
}
35+
}
36+
}
37+
return true;
38+
}
39+
};
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Check Completeness of a Binary Tree
2+
3+
Check if the given binary tree root forms a complete binary tree.
4+
5+
A complete binary tree is characterized by each level being fully filled except potentially the last level. However, all nodes in the last level should be aligned to the left. The last level, `h`, can have between `1` and `2h` nodes.
6+
\
7+
\
8+
\
9+
**Example 1:**
10+
>**Input:** root = [1,2,3,4,5,6]\
11+
>**Output:** true\
12+
>**Explanation:** All levels preceding the final one are completely filled (for instance, levels with node-values {1} and {2, 3}), and all nodes in the last level ({4, 5, 6}) are aligned to the left as much as possible.
13+
14+
**Example 2:**
15+
>**Input:** root = [1,2,3,4,5,null,7]\
16+
>**Output:** false\
17+
>**Explanation:** The node with value 7 isn't as far left as possible.
18+
19+
**Constraints:**
20+
21+
* The number of nodes in the tree is in the range ``[1, 100]``.
22+
* ``1 <= Node.val <= 1000``
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[1,2,3,4,5,6]
2+
true
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[1,2,3,5,6]
2+
true
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[1,2,3,5]
2+
true
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[1,2,3,4,5,null,7]
2+
false
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[1]
2+
true
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[1,null,2]
2+
false

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /