Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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.
* <p>
* 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<List<Integer>> levelOrder(TreeNode root) {
List<List<Integer>> res = new ArrayList<>();
Expand Down Expand Up @@ -39,10 +44,4 @@ public List<List<Integer>> 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]]
}
}
Original file line number Diff line number Diff line change
@@ -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<List<Integer>> expected = Arrays.asList(
List.of(3),
Arrays.asList(9, 20),
Arrays.asList(15, 7)
);

List<List<Integer>> result = assertTimeoutPreemptively(Duration.ofMillis(100), () -> solution.levelOrder(root));

assertEquals(expected, result);
}

@Test
void testLevelOrder_Example2() {
TreeNode root = new TreeNode(1);
List<List<Integer>> expected = List.of(
List.of(1)
);

List<List<Integer>> result = assertTimeoutPreemptively(Duration.ofMillis(100), () -> solution.levelOrder(root));

assertEquals(expected, result);
}

@Test
void testLevelOrder_Example3() {
List<List<Integer>> expected = new ArrayList<>();

List<List<Integer>> result = assertTimeoutPreemptively(Duration.ofMillis(100), () -> solution.levelOrder(null));

assertEquals(expected, result);
}
}
Loading