Skip to content

Commit 5c64a8e

Browse files
realDuYuanChaogithub-actions
andauthored
add leetcode (examplehub#137)
* remove-duplicates-from-sorted-array * fixme comment * test if binary tree is same add binary tree utils binary tree level order traversal * Formatted with Google Java Formatter Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
1 parent e8e99bc commit 5c64a8e

6 files changed

Lines changed: 88 additions & 1 deletion

File tree

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.examplehub.leetcode.easy;
2+
3+
import com.examplehub.leetcode.TreeNode;
4+
5+
/** https://leetcode.com/problems/same-tree/ */
6+
public class SameTree {
7+
public static boolean solution1(TreeNode p, TreeNode q) {
8+
if (p == null && q == null) {
9+
return true;
10+
}
11+
if (p == null || q == null) {
12+
return false;
13+
}
14+
if (p.val != q.val) {
15+
return false;
16+
}
17+
return solution1(p.left, q.left) && solution1(p.right, q.right);
18+
}
19+
}

src/main/java/com/examplehub/leetcode/middle/BinaryTreeLevelOrderTraversal.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import java.util.List;
77
import java.util.Queue;
88

9+
/** https://leetcode.com/problems/binary-tree-level-order-traversal/ */
910
public class BinaryTreeLevelOrderTraversal {
1011
public static List<List<Integer>> solution1(TreeNode root) {
1112
if (root == null) {
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.examplehub.leetcode.utils;
2+
3+
import com.examplehub.leetcode.TreeNode;
4+
5+
public class BinaryTreeUtils {
6+
public static TreeNode createTree(int[] nodes, int i) {
7+
int numberOfNodes = nodes.length;
8+
if (i >= numberOfNodes || nodes[i] == -1) {
9+
return null;
10+
}
11+
TreeNode root = new TreeNode(nodes[i]);
12+
root.left = createTree(nodes, i * 2 + 1);
13+
root.right = createTree(nodes, i * 2 + 2);
14+
return root;
15+
}
16+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.examplehub.leetcode.easy;
2+
3+
import static org.junit.jupiter.api.Assertions.*;
4+
5+
import com.examplehub.leetcode.TreeNode;
6+
import com.examplehub.leetcode.utils.BinaryTreeUtils;
7+
import org.junit.jupiter.api.Test;
8+
9+
class SameTreeTest {
10+
@Test
11+
void testSolution1() {
12+
TreeNode firstTree = BinaryTreeUtils.createTree(new int[] {1, 2, 3}, 0);
13+
TreeNode secondTree = BinaryTreeUtils.createTree(new int[] {1, 2, 3}, 0);
14+
assertTrue(SameTree.solution1(firstTree, secondTree));
15+
16+
firstTree = BinaryTreeUtils.createTree(new int[] {1, 2}, 0);
17+
secondTree = BinaryTreeUtils.createTree(new int[] {1, -1, 2}, 0);
18+
assertFalse(SameTree.solution1(firstTree, secondTree));
19+
20+
firstTree = BinaryTreeUtils.createTree(new int[] {1, 2, 1}, 0);
21+
secondTree = BinaryTreeUtils.createTree(new int[] {1, 1, 2}, 0);
22+
assertFalse(SameTree.solution1(firstTree, secondTree));
23+
24+
assertFalse(SameTree.solution1(null, firstTree));
25+
assertFalse(SameTree.solution1(null, secondTree));
26+
}
27+
}

src/test/java/com/examplehub/leetcode/middle/BinaryTreeLevelOrderTraversalTest.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,18 @@
22

33
import static org.junit.jupiter.api.Assertions.*;
44

5+
import com.examplehub.leetcode.TreeNode;
6+
import com.examplehub.leetcode.utils.BinaryTreeUtils;
57
import org.junit.jupiter.api.Test;
68

79
class BinaryTreeLevelOrderTraversalTest {
810
@Test
911
void testSolution1() {
10-
// TODO
12+
TreeNode root = BinaryTreeUtils.createTree(new int[] {3, 9, 20, -1, -1, 15, 7}, 0);
13+
assertEquals(
14+
"[[3], [9, 20], [15, 7]]", BinaryTreeLevelOrderTraversal.solution1(root).toString());
15+
16+
root = BinaryTreeUtils.createTree(new int[] {1}, 0);
17+
assertEquals("[[1]]", BinaryTreeLevelOrderTraversal.solution1(root).toString());
1118
}
1219
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.examplehub.leetcode.utils;
2+
3+
import static org.junit.jupiter.api.Assertions.*;
4+
5+
import com.examplehub.leetcode.TreeNode;
6+
import org.junit.jupiter.api.Test;
7+
8+
class BinaryTreeUtilsTest {
9+
@Test
10+
void test() {
11+
TreeNode root = BinaryTreeUtils.createTree(new int[] {1, 2, 3}, 0);
12+
assert root != null;
13+
assertEquals(1, root.val);
14+
assertEquals(2, root.left.val);
15+
assertEquals(3, root.right.val);
16+
}
17+
}

0 commit comments

Comments
 (0)