Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 015cc5f

Browse files
solves #429: N-ary Tree Level Order Traversal in java
1 parent 219f754 commit 015cc5f

File tree

3 files changed

+96
-0
lines changed

3 files changed

+96
-0
lines changed

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,7 @@
314314
| 414 | [Third Maximum Number](https://leetcode.com/problems/third-maximum-number) | [![Java](assets/java.png)](src/ThirdMaximumNumber.java) [![Python](assets/python.png)](python/fizz_buzz.py) | |
315315
| 415 | [Add Strings](https://leetcode.com/problems/add-strings) | [![Java](assets/java.png)](src/AddString.java) [![Python](assets/python.png)](python/add_strings.py) | |
316316
| 422 | 🔒 [Valid Word Square](https://leetcode.com/problems/valid-word-square) | | |
317+
| 429 | 🔒 [N-ary Tree Level Order Traversal](https://leetcode.com/problems/n-ary-tree-level-order-traversal) | [![Java](assets/java.png)](src/NAryTreeLevelOrderTraversal.java) | |
317318
| 430 | [Flatten a Multilevel Doubly Linked List](https://leetcode.com/problems/flatten-a-multilevel-doubly-linked-list) | [![Java](assets/java.png)](src/FlattenAMultiLevelDoublyLinkedList.java) | |
318319
| 434 | [Number of Segments in a String](https://leetcode.com/problems/number-of-segments-in-a-string) | [![Java](assets/java.png)](src/NumberOfSegmentsInString.java) [![Python](assets/python.png)](python/number_of_segments_in_a_string.py) | |
319320
| 441 | [Arranging Coins](https://leetcode.com/problems/arranging-coins) | [![Java](assets/java.png)](src/ArrangingCoins.java) [![Python](assets/python.png)](python/arranging_coins.py) | |

‎src/NAryTreeLevelOrderTraversal.java

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// https://leetcode.com/problems/n-ary-tree-level-order-traversal
2+
// T: O(N)
3+
// S: O(N)
4+
5+
import java.util.ArrayList;
6+
import java.util.LinkedList;
7+
import java.util.List;
8+
import java.util.Queue;
9+
10+
public class NAryTreeLevelOrderTraversal {
11+
public static class Node {
12+
public int val;
13+
public List<Node> children;
14+
15+
public Node() {}
16+
17+
public Node(int _val) {
18+
val = _val;
19+
}
20+
21+
public Node(int _val, List<Node> _children) {
22+
val = _val;
23+
children = _children;
24+
}
25+
}
26+
27+
public List<List<Integer>> levelOrder(Node root) {
28+
if (root == null) {
29+
return new ArrayList<>();
30+
}
31+
32+
final List<List<Integer>> result = new ArrayList<>();
33+
final Queue<Node> queue = new LinkedList<>();
34+
List<Integer> current = new ArrayList<>();
35+
36+
queue.add(root);
37+
queue.add(null);
38+
39+
while (!queue.isEmpty()) {
40+
final Node node = queue.poll();
41+
if (node == null) {
42+
result.add(current);
43+
current = new ArrayList<>();
44+
if (!queue.isEmpty()) {
45+
queue.add(null);
46+
}
47+
continue;
48+
}
49+
50+
current.add(node.val);
51+
queue.addAll(node.children);
52+
}
53+
54+
return result;
55+
}
56+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// https://leetcode.com/problems/paths-in-matrix-whose-sum-is-divisible-by-k
2+
// T: O()
3+
// S: O()
4+
5+
import java.util.LinkedList;
6+
import java.util.Queue;
7+
8+
public class PathsInMatrixWhoseSumIsDivisibleByK {
9+
private static final int MODULO = (int) (10e9 + 7);
10+
11+
public int numberOfPaths(int[][] grid, int k) {
12+
final int rows = grid.length, columns = grid[0].length;
13+
// row, column, currentSum
14+
final Queue<int[]> queue = new LinkedList<>();
15+
queue.add(new int[] {0, 0, grid[0][0]});
16+
17+
int paths = 0;
18+
19+
while (!queue.isEmpty()) {
20+
final int[] position = queue.poll();
21+
final int row = position[0], column = position[1], currentSum = position[2];
22+
if (row == rows - 1 && column == columns - 1) {
23+
if (currentSum % k == 0) {
24+
paths = (paths + 1) % MODULO;
25+
}
26+
continue;
27+
}
28+
29+
if (column + 1 < columns) {
30+
queue.add(new int[] { row, column + 1, currentSum + grid[row][column + 1]});
31+
}
32+
if (row + 1 < rows) {
33+
queue.add(new int[] { row + 1, column, currentSum + grid[row + 1][column]});
34+
}
35+
}
36+
37+
return paths;
38+
}
39+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /