Skip to content

Commit f12bcd0

Browse files
add 958
1 parent 98a4540 commit f12bcd0

File tree

3 files changed

+110
-0
lines changed

3 files changed

+110
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ _If you like this project, please leave me a star._ ★
137137
|966|[Vowel Spellchecker](https://leetcode.com/problems/vowel-spellchecker/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_966.java) | |Medium| Hash Table, String
138138
|965|[Univalued Binary Tree](https://leetcode.com/problems/univalued-binary-tree/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_965.java) | |Easy| DFS, recursion|
139139
|961|[N-Repeated Element in Size 2N Array](https://leetcode.com/problems/n-repeated-element-in-size-2n-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_961.java) | |Easy|
140+
|958|[Check Completeness of a Binary Tree](https://leetcode.com/problems/check-completeness-of-a-binary-tree/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_958.java) | |Medium|Tree
140141
|954|[Array of Doubled Pairs](https://leetcode.com/problems/array-of-doubled-pairs/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_954.java) | [:tv:](https://www.youtube.com/watch?v=y55NlhoOWe4)|Medium|
141142
|953|[Verifying an Alien Dictionary](https://leetcode.com/problems/verifying-an-alien-dictionary/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_953.java)| |Easy|
142143
|951|[Flip Equivalent Binary Trees](https://leetcode.com/problems/flip-equivalent-binary-trees/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_951.java) | |Medium| Tree, DFS, recursion|
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package com.fishercoder.solutions;
2+
3+
import com.fishercoder.common.classes.TreeNode;
4+
5+
import java.util.LinkedList;
6+
import java.util.Queue;
7+
8+
/**
9+
* 958. Check Completeness of a Binary Tree
10+
*
11+
* Given a binary tree, determine if it is a complete binary tree.
12+
* Definition of a complete binary tree from Wikipedia:
13+
* In a complete binary tree every level, except possibly the last, is completely filled, and all nodes in the last level are as far left as possible.
14+
* It can have between 1 and 2h nodes inclusive at the last level h.
15+
*
16+
* Example 1:
17+
* 1
18+
* / \
19+
* 2 3
20+
* / \ /
21+
* 4 5 6
22+
*
23+
* Input: [1,2,3,4,5,6]
24+
* Output: true
25+
* Explanation: Every level before the last is full (ie. levels with node-values {1} and {2, 3}),
26+
* and all nodes in the last level ({4, 5, 6}) are as far left as possible.
27+
*
28+
* Example 2:
29+
* 1
30+
* / \
31+
* 2 3
32+
* / \ \
33+
* 4 5 7
34+
* Input: [1,2,3,4,5,null,7]
35+
* Output: false
36+
* Explanation: The node with value 7 isn't as far left as possible.
37+
*
38+
* Note:
39+
* The tree will have between 1 and 100 nodes.
40+
* */
41+
public class _958 {
42+
public static class Solution1 {
43+
public boolean isCompleteTree(TreeNode root) {
44+
Queue<TreeNode> queue = new LinkedList<>();
45+
queue.offer(root);
46+
boolean shouldHaveNoMoreChildren = false;
47+
while (!queue.isEmpty()) {
48+
int size = queue.size();
49+
for (int i = 0; i < size; i++) {
50+
TreeNode curr = queue.poll();
51+
if (shouldHaveNoMoreChildren && (curr.left != null || curr.right != null)) {
52+
return false;
53+
}
54+
if (curr.left == null && curr.right != null) {
55+
return false;
56+
}
57+
if (curr.left != null) {
58+
queue.offer(curr.left);
59+
}
60+
if (curr.right == null) {
61+
shouldHaveNoMoreChildren = true;
62+
} else {
63+
queue.offer(curr.right);
64+
}
65+
}
66+
}
67+
return true;
68+
}
69+
}
70+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.common.classes.TreeNode;
4+
import com.fishercoder.common.utils.TreeUtils;
5+
import com.fishercoder.solutions._958;
6+
import org.junit.BeforeClass;
7+
import org.junit.Test;
8+
9+
import java.util.Arrays;
10+
11+
import static junit.framework.TestCase.assertEquals;
12+
13+
public class _958Test {
14+
private static _958.Solution1 solution1;
15+
16+
@BeforeClass
17+
public static void setup() {
18+
solution1 = new _958.Solution1();
19+
}
20+
21+
@Test
22+
public void test1() {
23+
TreeNode root = TreeUtils.constructBinaryTree(Arrays.asList(1, 2, 3, 4, 5, 6));
24+
assertEquals(true, solution1.isCompleteTree(root));
25+
}
26+
27+
@Test
28+
public void test2() {
29+
TreeNode root = TreeUtils.constructBinaryTree(Arrays.asList(1, 2, 3, 4, 5, null, 7));
30+
assertEquals(false, solution1.isCompleteTree(root));
31+
}
32+
33+
@Test
34+
public void test3() {
35+
TreeNode root = TreeUtils.constructBinaryTree(Arrays.asList(1, 2, 3, 4, 5, 8));
36+
assertEquals(true, solution1.isCompleteTree(root));
37+
}
38+
39+
}

0 commit comments

Comments
 (0)