File tree 2 files changed +81
-0
lines changed
main/java/com/smlnskgmail/jaman/leetcodejava/medium
test/java/com/smlnskgmail/jaman/leetcodejava/medium
2 files changed +81
-0
lines changed Original file line number Diff line number Diff line change
1
+ package com .smlnskgmail .jaman .leetcodejava .medium ;
2
+
3
+ import com .smlnskgmail .jaman .leetcodejava .support .TreeNode ;
4
+
5
+ // https://leetcode.com/problems/maximum-binary-tree-ii
6
+ public class MaximumBinaryTreeII {
7
+
8
+ private final TreeNode root ;
9
+ private final int val ;
10
+
11
+ public MaximumBinaryTreeII (TreeNode root , int val ) {
12
+ this .root = root ;
13
+ this .val = val ;
14
+ }
15
+
16
+ public TreeNode solution () {
17
+ return insertIntoMaxTree (root , val );
18
+ }
19
+
20
+ private TreeNode insertIntoMaxTree (TreeNode root , int val ) {
21
+ if (root == null ) {
22
+ return null ;
23
+ }
24
+ if (root .val < val ) {
25
+ var node = new TreeNode (val );
26
+ node .left = root ;
27
+ return node ;
28
+ } else {
29
+ if (root .right != null ) {
30
+ root .right = insertIntoMaxTree (root .right , val );
31
+ } else {
32
+ root .right = new TreeNode (val );
33
+ }
34
+ }
35
+ return root ;
36
+ }
37
+
38
+ }
Original file line number Diff line number Diff line change
1
+ package com .smlnskgmail .jaman .leetcodejava .medium ;
2
+
3
+ import com .smlnskgmail .jaman .leetcodejava .support .TreeNode ;
4
+ import org .junit .Test ;
5
+
6
+ import static org .junit .Assert .assertTrue ;
7
+
8
+ public class MaximumBinaryTreeIITest {
9
+
10
+ @ Test
11
+ public void defaultTest () {
12
+ assertTrue (
13
+ TreeNode .areEqual (
14
+ new TreeNode (
15
+ 5 ,
16
+ new TreeNode (
17
+ 4 ,
18
+ new TreeNode (1 ),
19
+ new TreeNode (
20
+ 3 ,
21
+ new TreeNode (2 ),
22
+ null
23
+ )
24
+ ),
25
+ null
26
+ ),
27
+ new MaximumBinaryTreeII (
28
+ new TreeNode (
29
+ 4 ,
30
+ new TreeNode (1 ),
31
+ new TreeNode (
32
+ 3 ,
33
+ new TreeNode (2 ),
34
+ null
35
+ )
36
+ ),
37
+ 5
38
+ ).solution ()
39
+ )
40
+ );
41
+ }
42
+
43
+ }
You can’t perform that action at this time.
0 commit comments