Repository navigation
Expand file tree
/
Copy pathBinaryTreeLevelOrderTraversal.java
More file actions
89 lines (82 loc) · 2.55 KB
/
Copy pathBinaryTreeLevelOrderTraversal.java
File metadata and controls
89 lines (82 loc) · 2.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;
import java.util.function.Function;
import java.util.stream.Collectors;
class Solution {
public static class Node<T> {
public T val;
public Node<T> left;
public Node<T> right;
public Node(T val) {
this(val, null, null);
}
public Node(T val, Node<T> left, Node<T> right) {
this.val = val;
this.left = left;
this.right = right;
}
}
// Given the root of a binary tree, return the level order traversal of its
// nodes' values (i.e., from left to right, level by level).
//
// Input: root = [3,9,20,null,null,15,7]
// Output: [[3],[9,20],[15,7]]
// Explanation:
// - Level 1: [3]
// - Level 2: [9, 20]
// - Level 3: [15, 7]
//
// Input: root = [1]
// Output: [[1]]
//
// Input: root = []
// Output: []
public static List<List<Integer>> levelOrderTraversal(Node<Integer> root) {
List<List<Integer>> result = new ArrayList<>();
if (root == null)
return result;
ArrayDeque<Node<Integer>> aq = new ArrayDeque<Node<Integer>>();
aq.add(root);
while (!aq.isEmpty()) {
List<Integer> temp = new ArrayList<>();
int levelSize = aq.size();
for (int i = 0; i < levelSize; i++) {
Node<Integer> front = aq.poll();
temp.add(front.val);
if (front.left != null)
aq.add(front.left);
if (front.right != null)
aq.add(front.right);
}
result.add(temp);
}
return result;
}
// this function builds a tree from input; you don't have to modify it
// learn more about how trees are encoded in
// https://algo.monster/problems/serializing_tree
public static <T> Node<T> buildTree(Iterator<String> iter, Function<String, T> f) {
String val = iter.next();
if (val.equals("x"))
return null;
Node<T> left = buildTree(iter, f);
Node<T> right = buildTree(iter, f);
return new Node<T>(f.apply(val), left, right);
}
public static List<String> splitWords(String s) {
return s.isEmpty() ? List.of() : Arrays.asList(s.split(" "));
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Node<Integer> root = buildTree(splitWords(scanner.nextLine()).iterator(), Integer::parseInt);
scanner.close();
List<List<Integer>> res = levelOrderTraversal(root);
for (List<Integer> row : res) {
System.out.println(row.stream().map(String::valueOf).collect(Collectors.joining(" ")));
}
}
}