Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit ebb4e68

Browse files
remove TreeLinkNode
1 parent 5a438cf commit ebb4e68

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

-16
This file was deleted.

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

+18-9
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,30 @@
11
package com.fishercoder.solutions;
22

3-
import com.fishercoder.common.classes.TreeLinkNode;
4-
53
import java.util.LinkedList;
64
import java.util.Queue;
75

86
public class _116 {
7+
public static class Node {
8+
public int val;
9+
public Node left;
10+
public Node right;
11+
public Node next;
12+
13+
public Node(int x) {
14+
this.val = x;
15+
}
16+
}
17+
918
public static class Solution1 {
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-
public void connect(TreeLinkNode root) {
23+
public void connect(Node root) {
1524

16-
TreeLinkNode head = null; //head of the next level
17-
TreeLinkNode prev = null; //the leading node on the next level
18-
TreeLinkNode curr = root; //current node of current level
25+
Node head = null; //head of the next level
26+
Node prev = null; //the leading node on the next level
27+
Node curr = 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-
public TreeLinkNode connect(TreeLinkNode root) {
64+
public Node connect(Node root) {
5665
if (root == null) {
5766
return null;
5867
}
59-
Queue<TreeLinkNode> queue = new LinkedList<>();
68+
Queue<Node> queue = new LinkedList<>();
6069
queue.offer(root);
6170
while (!queue.isEmpty()) {
6271
int size = queue.size();
6372
for (int i = 0; i < size; i++) {
64-
TreeLinkNode curr = queue.poll();
73+
Node curr = queue.poll();
6574
if (i < size - 1) {
6675
curr.next = queue.peek();
6776
} else {

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

+17-6
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,28 @@
11
package com.fishercoder.solutions;
22

3-
import com.fishercoder.common.classes.TreeLinkNode;
4-
53
public class _117 {
4+
5+
public static class Node {
6+
public int val;
7+
public Node left;
8+
public Node right;
9+
public Node next;
10+
11+
public Node(int x) {
12+
this.val = x;
13+
}
14+
}
15+
616
public static class Solution1 {
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-
public void connect(TreeLinkNode root) {
21+
public Node connect(Node root) {
1222

13-
TreeLinkNode head = null; //head of the next level
14-
TreeLinkNode prev = null; //the leading node on the next level
15-
TreeLinkNode cur = root; //current node of current level
23+
Node head = null; //head of the next level
24+
Node prev = null; //the leading node on the next level
25+
Node cur = 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+
return root;
4758
}
4859
}
4960
}

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

+13-15
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
package com.fishercoder;
22

3-
import com.fishercoder.common.classes.TreeLinkNode;
43
import com.fishercoder.solutions._116;
54
import org.junit.BeforeClass;
65
import org.junit.Test;
76

87
public class _116Test {
98
private static _116.Solution1 solution1;
109
private static _116.Solution2 solution2;
11-
private static TreeLinkNode root;
10+
private static _116.Node root;
1211

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

1918
@Test
2019
public void test1() {
21-
root = new TreeLinkNode(1);
22-
root.left = new TreeLinkNode(2);
23-
root.right = new TreeLinkNode(3);
24-
root.left.left = new TreeLinkNode(4);
25-
root.left.right = new TreeLinkNode(5);
26-
root.right.right = new TreeLinkNode(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
public void test2() {
33-
root = new TreeLinkNode(1);
34-
root.left = new TreeLinkNode(2);
35-
root.right = new TreeLinkNode(3);
36-
root.left.left = new TreeLinkNode(4);
37-
root.left.right = new TreeLinkNode(5);
38-
root.right.right = new TreeLinkNode(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

+7-8
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
package com.fishercoder;
22

3-
import com.fishercoder.common.classes.TreeLinkNode;
43
import com.fishercoder.solutions._117;
54
import org.junit.BeforeClass;
65
import org.junit.Test;
76

87
public class _117Test {
98
private static _117.Solution1 solution1;
10-
private static TreeLinkNode root;
9+
private static _117.Node root;
1110

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

1716
@Test
1817
public void test1() {
19-
root = new TreeLinkNode(1);
20-
root.left = new TreeLinkNode(2);
21-
root.right = new TreeLinkNode(3);
22-
root.left.left = new TreeLinkNode(4);
23-
root.left.right = new TreeLinkNode(5);
24-
root.right.right = new TreeLinkNode(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)