8000 [LEET-515] add 515 · aduispace/Leetcode-1@9a9d980 · GitHub
[go: up one dir, main page]

Skip to content

Commit 9a9d980

Browse files
[LEET-515] add 515
1 parent 4b081cd commit 9a9d980

File tree

3 files changed

+99
-0
lines changed

3 files changed

+99
-0
lines changed

leetcode-algorithms/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## Algorithms
44
| # | Title | Solutions | Time | Space | Difficulty | Tag | Notes
55
|-----|----------------|---------------|---------------|---------------|-------------|--------------|-----
6+
|515|[Find Largest Element in Each Row](https://leetcode.com/problems/find-largest-element-in-each-row/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/FindLargestElementinEachRow.java) | O(n) |O(k) | Medium| BFS
67
|513|[Find Left Most Element](https://leetcode.com/problems/find-left-most-element/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/FindLeftMostElement.java) | O(n) |O(k) | Medium| BFS
78
|504|[Base 7](https://leetcode.com/problems/base-7/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/Base7.java) | O(1) |O(1) | Easy|
89
|501|[Find Mode in Binary Tree](https://leetcode.com/problems/find-mode-in-binary-tree/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/FindModeinBinaryTree.java) | O(n) |O(k) | Easy| Binary Tree
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.stevesun.solutions;
2+
3+
import com.stevesun.common.classes.TreeNode;
4+
5+
import java.util.ArrayList;
6+
import java.util.LinkedList;
7+
import java.util.List;
8+
import java.util.Queue;
9+
10+
/**
11+
* You need to find the largest value in each row of a binary tree.
12+
13+
Example:
14+
Input:
15+
16+
1
17+
/ \
18+
3 2
19+
/ \ \
20+
5 3 9
21+
22+
Output: [1, 3, 9]
23+
*/
24+
public class FindLargestElementinEachRow {
25+
26+
public int[] findValueMostElement(TreeNode root) {
27+
Queue<TreeNode> queue = new LinkedList<>();
28+
if (root != null) {
29+
queue.offer(root);
30+
List<Integer> list = new ArrayList<>();
31+
while (!queue.isEmpty()) {
32+
int max = Integer.MIN_VALUE;
33+
int size = queue.size();
34+
for (int i = 0; i < size; i++) {
35+
TreeNode curr = queue.poll();
36+
max = Math.max(max, curr.val);
37+
if (curr.left != null) queue.offer(curr.left);
38+
if (curr.right != null) queue.offer(curr.right);
39+
}
40+
list.add(max);
41+
}
42+
int[] result = new int[list.size()];
43+
for (int i = 0; i < list.size(); i++) {
44+
result[i] = list.get(i);
45+
}
46+
return result;
47+
} else {
48+
return new int[]{};
49+
}
50+
}
51+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.stevesun;
2+
3+
import com.stevesun.common.classes.TreeNode;
4+
import com.stevesun.solutions.FindLargestElementinEachRow;
5+
import org.junit.Before;
6+
import org.junit.BeforeClass;
7+
import org.junit.Test;
8+
9+
import static org.junit.Assert.assertArrayEquals;
10+
11+
public class FindLargestElementinEachRowTest {
12+
private static FindLargestElementinEachRow test;
13+
private static int[] expected;
14+
private static int[] actual;
15+
private static TreeNode root;
16+
17+
@BeforeClass
18+
public static void setup(){
19+
test = new FindLargestElementinEachRow();
20+
}
21+
22+
@Before
23+
public void setupForEachTest(){
24+
expected = new int[]{};
25+
actual = new int[]{};
26+
root = new TreeNode(0);
27+
}
28+
29+
@Test
30+
public void test1(){
31+
TreeNode root = new TreeNode(1);
32+
root.left = new TreeNode(3);
33+
root.right= new TreeNode(2);
34+
expected = new int[]{1, 3};
35+
actual = test.findValueMostElement(root);
36+
assertArrayEquals(expected, actual);
37+
38+
}
39+
40+
@Test
41+
public void test2(){
42+
expected = new int[]{};
43+
actual = test.findValueMostElement(null);
44+
assertArrayEquals(expected, actual);
45+
46+
}
47+
}

0 commit comments

Comments
 (0)
0