Skip to content

Commit 6e51450

Browse files
removes TreeNode dependency
1 parent b1246c4 commit 6e51450

13 files changed

+3
-122
lines changed

src/BalancedBinaryTree.java

-5
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
11
public class BalancedBinaryTree {
2-
private static class TreeNode {
3-
int val;
4-
TreeNode left;
5-
TreeNode right;
6-
}
72

83
public static boolean isBalanced(TreeNode root) {
94
return height(root) != -1;

src/BinaryTreeLevelOrderTraversalII.java

-5
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,6 @@
55
import java.util.Queue;
66

77
public class BinaryTreeLevelOrderTraversalII {
8-
private static class TreeNode {
9-
int val;
10-
TreeNode left;
11-
TreeNode right;
12-
}
138

149
private static class Pair {
1510
TreeNode tree;

src/BinaryTreePaths.java

+1-15
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,10 @@
22
import java.util.List;
33

44
public class BinaryTreePaths {
5-
public static void main(String[] args) {
6-
String[] array = {"hello", "1", "2"};
7-
List<CharSequence> list = new ArrayList<>();
8-
list.add("1");
9-
list.add("2");
10-
list.add("3");
11-
System.out.println(String.join("->", list));
12-
}
13-
14-
private static class TreeNode {
15-
int val;
16-
TreeNode left;
17-
TreeNode right;
18-
}
195

206
public static List<String> binaryTreePaths(TreeNode root) {
217
List<String> result = new ArrayList<>();
22-
binaryTreePaths(root, new ArrayList<CharSequence>(), result);
8+
binaryTreePaths(root, new ArrayList<>(), result);
239
return result;
2410
}
2511

src/ConvertSortedArrayToBinarySearchTree.java

-15
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,4 @@
11
public class ConvertSortedArrayToBinarySearchTree {
2-
private static class TreeNode {
3-
int val;
4-
TreeNode left;
5-
TreeNode right;
6-
7-
TreeNode(int val) {
8-
this.val = val;
9-
}
10-
11-
TreeNode(int val, TreeNode left, TreeNode right) {
12-
this.val = val;
13-
this.left = left;
14-
this.right = right;
15-
}
16-
}
172

183
public static TreeNode sortedArrayToBST(int[] array) {
194
return sortedArrayToBST(array, 0, array.length);

src/FindModeInBinarySearchTree.java

-13
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,6 @@
33
import java.util.List;
44

55
public class FindModeInBinarySearchTree {
6-
static class TreeNode {
7-
int val;
8-
TreeNode left;
9-
TreeNode right;
10-
TreeNode() {}
11-
TreeNode(int val) { this.val = val; }
12-
TreeNode(int val, TreeNode left, TreeNode right) {
13-
this.val = val;
14-
this.left = left;
15-
this.right = right;
16-
}
17-
}
18-
196
private static TreeNode previous;
207
private static int count = 1;
218
private static int mode = 0;

src/InvertBinaryTree.java

-5
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
11
public class InvertBinaryTree {
2-
private static class TreeNode {
3-
int val;
4-
TreeNode left;
5-
TreeNode right;
6-
}
72

83
public static TreeNode invertTree(TreeNode root) {
94
if (root == null) {

src/LowestCommonAncestorOfBinarySearchTree.java

-5
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,6 @@
22
import java.util.List;
33

44
public class LowestCommonAncestorOfBinarySearchTree {
5-
private static class TreeNode {
6-
int val;
7-
TreeNode left;
8-
TreeNode right;
9-
}
105

116
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
127
List<TreeNode> pathToP = pathTo(root, p);

src/MaximumDepthOfBinaryTree.java

-6
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,4 @@
11
public class MaximumDepthOfBinaryTree {
2-
private static class TreeNode {
3-
int val;
4-
TreeNode left;
5-
TreeNode right;
6-
}
7-
82
public int maxDepth(TreeNode root) {
93
if (root == null) {
104
return 0;

src/MinimumDepthOfBinaryTree.java

-5
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
11
public class MinimumDepthOfBinaryTree {
2-
private static class TreeNode {
3-
int val;
4-
TreeNode left;
5-
TreeNode right;
6-
}
72

83
public static int minDepth(TreeNode root) {
94
if (root == null) {

src/PathSum.java

-5
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
11
public class PathSum {
2-
private static class TreeNode {
3-
int val;
4-
TreeNode left;
5-
TreeNode right;
6-
}
72

83
public static boolean hasPathSum(TreeNode root, int sum) {
94
return root != null && hasPathSum(root, sum, root.val);

src/SameTree.java

-5
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
11
public class SameTree {
2-
private static class TreeNode {
3-
int val;
4-
TreeNode left;
5-
TreeNode right;
6-
}
72

83
public boolean isSameTree(TreeNode p, TreeNode q) {
94
if (p == null && q == null) {

src/SymmetricTree.java

-5
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
11
public class SymmetricTree {
2-
private static class TreeNode {
3-
int val;
4-
TreeNode left;
5-
TreeNode right;
6-
}
72

83
public boolean isSymmetric(TreeNode root) {
94
return root == null || isSymmetric(root.left, root.right);

src/UniqueBinarySearchTreesII.java

+2-33
Original file line numberDiff line numberDiff line change
@@ -2,47 +2,16 @@
22
import java.util.List;
33

44
public class UniqueBinarySearchTreesII {
5-
private static class TreeNode implements TreePrinter.PrintableNode {
6-
int val;
7-
TreeNode left;
8-
TreeNode right;
9-
10-
TreeNode(int val) {
11-
this.val = val;
12-
}
13-
14-
TreeNode(int val, TreeNode left, TreeNode right) {
15-
this.val = val;
16-
this.left = left;
17-
this.right = right;
18-
}
19-
20-
@Override
21-
public TreePrinter.PrintableNode getLeft() {
22-
return left;
23-
}
24-
25-
@Override
26-
public TreePrinter.PrintableNode getRight() {
27-
return right;
28-
}
29-
30-
@Override
31-
public String getText() {
32-
return val + "";
33-
}
34-
}
35-
365
public static List<TreeNode> generateTrees(int n) {
376
List<TreeNode>[] result = new List[n + 1];
38-
result[0] = new ArrayList<TreeNode>();
7+
result[0] = new ArrayList<>();
398
if (n == 0) {
409
return result[0];
4110
}
4211

4312
result[0].add(null);
4413
for (int len = 1; len <= n; len++) {
45-
result[len] = new ArrayList<TreeNode>();
14+
result[len] = new ArrayList<>();
4615
for (int j = 0; j < len; j++) {
4716
for (TreeNode nodeL : result[j]) {
4817
for (TreeNode nodeR : result[len - j - 1]) {

0 commit comments

Comments
 (0)