Movatterモバイル変換


[0]ホーム

URL:


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

Commitebb4e68

Browse files
remove TreeLinkNode
1 parent5a438cf commitebb4e68

File tree

5 files changed

+55
-54
lines changed

5 files changed

+55
-54
lines changed

‎src/main/java/com/fishercoder/common/classes/TreeLinkNode.java

Lines changed: 0 additions & 16 deletions
This file was deleted.

‎src/main/java/com/fishercoder/solutions/_116.java

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,30 @@
11
packagecom.fishercoder.solutions;
22

3-
importcom.fishercoder.common.classes.TreeLinkNode;
4-
53
importjava.util.LinkedList;
64
importjava.util.Queue;
75

86
publicclass_116 {
7+
publicstaticclassNode {
8+
publicintval;
9+
publicNodeleft;
10+
publicNoderight;
11+
publicNodenext;
12+
13+
publicNode(intx) {
14+
this.val =x;
15+
}
16+
}
17+
918
publicstaticclassSolution1 {
1019
/**
1120
* credit: https://discuss.leetcode.com/topic/1106/o-1-space-o-n-complexity-iterative-solution
1221
* based on level order traversal
1322
*/
14-
publicvoidconnect(TreeLinkNoderoot) {
23+
publicvoidconnect(Noderoot) {
1524

16-
TreeLinkNodehead =null;//head of the next level
17-
TreeLinkNodeprev =null;//the leading node on the next level
18-
TreeLinkNodecurr =root;//current node of current level
25+
Nodehead =null;//head of the next level
26+
Nodeprev =null;//the leading node on the next level
27+
Nodecurr =root;//current node of current level
1928

2029
while (curr !=null) {
2130
while (curr !=null) {//iterate on the current level
@@ -52,16 +61,16 @@ public static class Solution2 {
5261
/**
5362
* My complete original solution on 10/10/2021.
5463
*/
55-
publicTreeLinkNodeconnect(TreeLinkNoderoot) {
64+
publicNodeconnect(Noderoot) {
5665
if (root ==null) {
5766
returnnull;
5867
}
59-
Queue<TreeLinkNode>queue =newLinkedList<>();
68+
Queue<Node>queue =newLinkedList<>();
6069
queue.offer(root);
6170
while (!queue.isEmpty()) {
6271
intsize =queue.size();
6372
for (inti =0;i <size;i++) {
64-
TreeLinkNodecurr =queue.poll();
73+
Nodecurr =queue.poll();
6574
if (i <size -1) {
6675
curr.next =queue.peek();
6776
}else {

‎src/main/java/com/fishercoder/solutions/_117.java

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,28 @@
11
packagecom.fishercoder.solutions;
22

3-
importcom.fishercoder.common.classes.TreeLinkNode;
4-
53
publicclass_117 {
4+
5+
publicstaticclassNode {
6+
publicintval;
7+
publicNodeleft;
8+
publicNoderight;
9+
publicNodenext;
10+
11+
publicNode(intx) {
12+
this.val =x;
13+
}
14+
}
15+
616
publicstaticclassSolution1 {
717
/**
818
* credit: https://discuss.leetcode.com/topic/1106/o-1-space-o-n-complexity-iterative-solution
919
* O(1) space, based on level order traversal
1020
*/
11-
publicvoidconnect(TreeLinkNoderoot) {
21+
publicNodeconnect(Noderoot) {
1222

13-
TreeLinkNodehead =null;//head of the next level
14-
TreeLinkNodeprev =null;//the leading node on the next level
15-
TreeLinkNodecur =root;//current node of current level
23+
Nodehead =null;//head of the next level
24+
Nodeprev =null;//the leading node on the next level
25+
Nodecur =root;//current node of current level
1626

1727
while (cur !=null) {
1828

@@ -44,6 +54,7 @@ public void connect(TreeLinkNode root) {
4454
head =null;
4555
prev =null;
4656
}
57+
returnroot;
4758
}
4859
}
4960
}

‎src/test/java/com/fishercoder/_116Test.java

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
packagecom.fishercoder;
22

3-
importcom.fishercoder.common.classes.TreeLinkNode;
43
importcom.fishercoder.solutions._116;
54
importorg.junit.BeforeClass;
65
importorg.junit.Test;
76

87
publicclass_116Test {
98
privatestatic_116.Solution1solution1;
109
privatestatic_116.Solution2solution2;
11-
privatestaticTreeLinkNoderoot;
10+
privatestatic_116.Noderoot;
1211

1312
@BeforeClass
1413
publicstaticvoidsetup() {
@@ -18,24 +17,23 @@ public static void setup() {
1817

1918
@Test
2019
publicvoidtest1() {
21-
root =newTreeLinkNode(1);
22-
root.left =newTreeLinkNode(2);
23-
root.right =newTreeLinkNode(3);
24-
root.left.left =newTreeLinkNode(4);
25-
root.left.right =newTreeLinkNode(5);
26-
root.right.right =newTreeLinkNode(7);
27-
20+
root =new_116.Node(1);
21+
root.left =new_116.Node(2);
22+
root.right =new_116.Node(3);
23+
root.left.left =new_116.Node(4);
24+
root.left.right =new_116.Node(5);
25+
root.right.right =new_116.Node(7);
2826
solution1.connect(root);
2927
}
3028

3129
@Test
3230
publicvoidtest2() {
33-
root =newTreeLinkNode(1);
34-
root.left =newTreeLinkNode(2);
35-
root.right =newTreeLinkNode(3);
36-
root.left.left =newTreeLinkNode(4);
37-
root.left.right =newTreeLinkNode(5);
38-
root.right.right =newTreeLinkNode(7);
31+
root =new_116.Node(1);
32+
root.left =new_116.Node(2);
33+
root.right =new_116.Node(3);
34+
root.left.left =new_116.Node(4);
35+
root.left.right =new_116.Node(5);
36+
root.right.right =new_116.Node(7);
3937

4038
solution2.connect(root);
4139
}

‎src/test/java/com/fishercoder/_117Test.java

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
packagecom.fishercoder;
22

3-
importcom.fishercoder.common.classes.TreeLinkNode;
43
importcom.fishercoder.solutions._117;
54
importorg.junit.BeforeClass;
65
importorg.junit.Test;
76

87
publicclass_117Test {
98
privatestatic_117.Solution1solution1;
10-
privatestaticTreeLinkNoderoot;
9+
privatestatic_117.Noderoot;
1110

1211
@BeforeClass
1312
publicstaticvoidsetup() {
@@ -16,12 +15,12 @@ public static void setup() {
1615

1716
@Test
1817
publicvoidtest1() {
19-
root =newTreeLinkNode(1);
20-
root.left =newTreeLinkNode(2);
21-
root.right =newTreeLinkNode(3);
22-
root.left.left =newTreeLinkNode(4);
23-
root.left.right =newTreeLinkNode(5);
24-
root.right.right =newTreeLinkNode(7);
18+
root =new_117.Node(1);
19+
root.left =new_117.Node(2);
20+
root.right =new_117.Node(3);
21+
root.left.left =new_117.Node(4);
22+
root.left.right =new_117.Node(5);
23+
root.right.right =new_117.Node(7);
2524

2625
solution1.connect(root);
2726
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp