diff --git a/src/BinaryTreeLevelOrderTraversal102.java b/src/main/java/com/leetcode/medium/BinaryTreeLevelOrderTraversal102.java similarity index 55% rename from src/BinaryTreeLevelOrderTraversal102.java rename to src/main/java/com/leetcode/medium/BinaryTreeLevelOrderTraversal102.java index b787519..eae661d 100644 --- a/src/BinaryTreeLevelOrderTraversal102.java +++ b/src/main/java/com/leetcode/medium/BinaryTreeLevelOrderTraversal102.java @@ -1,17 +1,22 @@ +// Tags: Tree, BFS +package com.leetcode.medium; + +import com.leetcode.datastructure.TreeNode; + import java.util.ArrayList; import java.util.LinkedList; import java.util.List; import java.util.Queue; public class BinaryTreeLevelOrderTraversal102 { - /* - Time complexity : O(N) since each node is processed exactly once. - - Space complexity: The space complexity for BFS is O(w) where w is the maximum width of the tree. - Due to the nature of BFS, at any given moment, the queue holds no more than two levels of nodes. - In the worst case, a level in a full binary tree contains at most half of the total nodes - (i.e. N/2), i.e. this is also the level where the leaf nodes reside. - Hence, the overall space complexity of the algorithm is O(N). (/2 could be ignored) + /** + * Time complexity: O(N) since each node is processed exactly once. + *
+ * Space complexity: The space complexity for BFS is O(w) where w is the maximum width of the tree.
+ * Due to the nature of BFS, at any given moment, the queue holds no more than two levels of nodes.
+ * In the worst case, a level in a full binary tree contains at most half of the total nodes
+ * (i.e. N/2), i.e. this is also the level where the leaf nodes reside.
+ * Hence, the overall space complexity of the algorithm is O(N). (/2 could be ignored)
*/
public List> levelOrder(TreeNode root) {
List
> res = new ArrayList<>();
@@ -39,10 +44,4 @@ public List
> levelOrder(TreeNode root) {
}
return res;
}
-
- public static void main(String[] args) {
- TreeNode root = new TreeNode(3, new TreeNode(9), new TreeNode(20, new TreeNode(15), new TreeNode(7)));
- System.out.println(new BinaryTreeLevelOrderTraversal102().levelOrder(root));
- // [[3], [9, 20], [15, 7]]
- }
}
diff --git a/src/test/java/com/leetcode/medium/BinaryTreeLevelOrderTraversal102Test.java b/src/test/java/com/leetcode/medium/BinaryTreeLevelOrderTraversal102Test.java
new file mode 100644
index 0000000..1a6bf21
--- /dev/null
+++ b/src/test/java/com/leetcode/medium/BinaryTreeLevelOrderTraversal102Test.java
@@ -0,0 +1,61 @@
+package com.leetcode.medium;
+
+import com.leetcode.datastructure.TreeNode;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+import java.time.Duration;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertTimeoutPreemptively;
+
+public class BinaryTreeLevelOrderTraversal102Test {
+ private BinaryTreeLevelOrderTraversal102 solution;
+
+ @BeforeEach
+ void setUp() {
+ solution = new BinaryTreeLevelOrderTraversal102();
+ }
+
+ @Test
+ void testLevelOrder_Example1() {
+ TreeNode root = new TreeNode(3);
+ root.left = new TreeNode(9);
+ root.right = new TreeNode(20);
+ root.right.left = new TreeNode(15);
+ root.right.right = new TreeNode(7);
+ List
> expected = Arrays.asList(
+ List.of(3),
+ Arrays.asList(9, 20),
+ Arrays.asList(15, 7)
+ );
+
+ List
> result = assertTimeoutPreemptively(Duration.ofMillis(100), () -> solution.levelOrder(root));
+
+ assertEquals(expected, result);
+ }
+
+ @Test
+ void testLevelOrder_Example2() {
+ TreeNode root = new TreeNode(1);
+ List
> expected = List.of(
+ List.of(1)
+ );
+
+ List
> result = assertTimeoutPreemptively(Duration.ofMillis(100), () -> solution.levelOrder(root));
+
+ assertEquals(expected, result);
+ }
+
+ @Test
+ void testLevelOrder_Example3() {
+ List
> expected = new ArrayList<>();
+
+ List
> result = assertTimeoutPreemptively(Duration.ofMillis(100), () -> solution.levelOrder(null));
+
+ assertEquals(expected, result);
+ }
+}