Skip to content

Commit 85c0b4a

Browse files
committed
#102 二叉树的分层遍历
1 parent c657d21 commit 85c0b4a

File tree

2 files changed

+112
-0
lines changed

2 files changed

+112
-0
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package tree;
2+
3+
import java.util.ArrayList;
4+
import java.util.LinkedList;
5+
import java.util.List;
6+
import java.util.Queue;
7+
8+
/**
9+
* @author alis
10+
* @date 2019/11/1 12:17 AM
11+
* @description
12+
*/
13+
public class LC_102_BinaryTreeLevelOrder {
14+
15+
List<List<Integer>> result = new ArrayList<>();
16+
17+
18+
/**
19+
* BFS
20+
* 时间复杂度:O(n)
21+
*/
22+
public List<List<Integer>> levelOrderBFS(TreeNode root) {
23+
helper(root, 0);
24+
return result;
25+
}
26+
27+
private void helper(TreeNode root, int level) {
28+
// init level 0
29+
if (level == result.size())
30+
result.add(new ArrayList<>());
31+
// process current
32+
result.get(level).add(root.val);
33+
// drill down
34+
if (null != root.left)
35+
helper(root.left, level + 1); // 同一个level层的数据都会同样处理,并只处理一遍
36+
if (null != root.right)
37+
helper(root.right, level + 1);
38+
}
39+
40+
public List<List<Integer>> levelOrderLoop(TreeNode root) {
41+
if (null == root) return result;
42+
Queue<TreeNode> queue = new LinkedList<>();
43+
queue.add(root);
44+
while (!queue.isEmpty()) {
45+
int size = queue.size();
46+
List<Integer> current = new ArrayList<>();
47+
while (size-- > 0) {
48+
TreeNode node = queue.poll();
49+
current.add(node.val);
50+
if (node.left != null) queue.add(node.left);
51+
if (node.right != null) queue.add(node.right);
52+
}
53+
result.add(current);
54+
}
55+
return result;
56+
}
57+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package tree;
2+
3+
import org.junit.Test;
4+
5+
import java.util.ArrayList;
6+
import java.util.Arrays;
7+
import java.util.Collections;
8+
import java.util.List;
9+
10+
import static org.hamcrest.core.Is.is;
11+
import static org.junit.Assert.assertThat;
12+
13+
/**
14+
* @author alis
15+
* @date 2019/11/1 12:09 AM
16+
* @description
17+
*/
18+
public class LC_102_BinaryTreeLevelOrderTest {
19+
20+
private LC_102_BinaryTreeLevelOrder binaryTreeLevelOrder = new LC_102_BinaryTreeLevelOrder();
21+
22+
@Test
23+
public void testLevelOrderRecur() {
24+
TreeNode root3 = new TreeNode(3);
25+
TreeNode right20 = new TreeNode(20);
26+
right20.left = new TreeNode(15);
27+
right20.right = new TreeNode(7);
28+
root3.left = new TreeNode(9);
29+
root3.right = right20;
30+
List<List<Integer>> result = binaryTreeLevelOrder.levelOrderBFS(root3);
31+
List<List<Integer>> except = new ArrayList<>();
32+
except.add(Collections.singletonList(3));
33+
except.add(Arrays.asList(9, 20));
34+
except.add(Arrays.asList(15, 7));
35+
assertThat(result, is(except));
36+
37+
}
38+
39+
@Test
40+
public void testLevelOrderLoop() {
41+
TreeNode root3 = new TreeNode(3);
42+
TreeNode right20 = new TreeNode(20);
43+
right20.left = new TreeNode(15);
44+
right20.right = new TreeNode(7);
45+
root3.left = new TreeNode(9);
46+
root3.right = right20;
47+
List<List<Integer>> result = binaryTreeLevelOrder.levelOrderLoop(root3);
48+
List<List<Integer>> except = new ArrayList<>();
49+
except.add(Collections.singletonList(3));
50+
except.add(Arrays.asList(9, 20));
51+
except.add(Arrays.asList(15, 7));
52+
assertThat(result, is(except));
53+
54+
}
55+
}

0 commit comments

Comments
 (0)