File tree Expand file tree Collapse file tree 3 files changed +99
-0
lines changed
main/java/com/stevesun/solutions Expand file tree Collapse file tree 3 files changed +99
-0
lines changed Original file line number Diff line number Diff line change 3
3
## Algorithms
4
4
| # | Title | Solutions | Time | Space | Difficulty | Tag | Notes
5
5
|-----|----------------|---------------|---------------|---------------|-------------|--------------|-----
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
6
7
|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
7
8
| 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|
8
9
|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
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments