Skip to content
This repository was archived by the owner on May 5, 2025. It is now read-only.

Commit 552d11c

Browse files
committed
adds java inorder
1 parent 8f911f6 commit 552d11c

File tree

6 files changed

+226
-0
lines changed

6 files changed

+226
-0
lines changed
+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
2+
questions.md
3+
target
4+
.vscode
5+
.settings
6+
.project
7+
.classpath
8+
glue.sh

java/binary_tree/inorder/Dockerfile

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
2+
FROM maven:latest
3+
4+
WORKDIR /inorder
5+
6+
COPY pom.xml pom.xml
7+
8+
COPY src src
9+
10+
RUN mvn package
11+

java/binary_tree/inorder/pom.xml

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>com.7astro7.algo</groupId>
8+
<artifactId>inorder</artifactId>
9+
<version>1.0</version>
10+
11+
<name>inorder</name>
12+
<url>https://github.com/7astro7/leetcode</url>
13+
14+
<properties>
15+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
16+
<maven.compiler.source>15</maven.compiler.source>
17+
<maven.compiler.target>15</maven.compiler.target>
18+
</properties>
19+
20+
<dependencies>
21+
<dependency>
22+
<groupId>junit</groupId>
23+
<artifactId>junit</artifactId>
24+
<version>4.13.1</version>
25+
<scope>test</scope>
26+
</dependency>
27+
</dependencies>
28+
29+
<build>
30+
<plugins>
31+
<plugin>
32+
<artifactId>maven-clean-plugin</artifactId>
33+
<version>3.1.0</version>
34+
</plugin>
35+
<plugin>
36+
<artifactId>maven-resources-plugin</artifactId>
37+
<version>3.0.2</version>
38+
</plugin>
39+
<plugin>
40+
<artifactId>maven-compiler-plugin</artifactId>
41+
<version>3.8.0</version>
42+
</plugin>
43+
<plugin>
44+
<artifactId>maven-surefire-plugin</artifactId>
45+
<version>2.22.1</version>
46+
</plugin>
47+
<plugin>
48+
<artifactId>maven-jar-plugin</artifactId>
49+
<version>3.0.2</version>
50+
</plugin>
51+
<plugin>
52+
<artifactId>maven-install-plugin</artifactId>
53+
<version>2.5.2</version>
54+
</plugin>
55+
<plugin>
56+
<artifactId>maven-deploy-plugin</artifactId>
57+
<version>2.8.2</version>
58+
</plugin>
59+
<plugin>
60+
<artifactId>maven-site-plugin</artifactId>
61+
<version>3.7.1</version>
62+
</plugin>
63+
<plugin>
64+
<artifactId>maven-project-info-reports-plugin</artifactId>
65+
<version>3.0.0</version>
66+
</plugin>
67+
</plugins>
68+
</pluginManagement>
69+
</build>
70+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package leetInorder;
2+
import java.util.List;
3+
import java.util.ArrayList;
4+
import leetInorder.TreeNode;
5+
6+
public class Solution {
7+
8+
/**
9+
Given the root of a binary tree, return the inorder traversal of its nodes' values.
10+
11+
Example 1:
12+
Input: root = [1,null,2,3]
13+
Output: [1,3,2]
14+
15+
Example 2:
16+
Input: root = []
17+
Output: []
18+
19+
Example 3:
20+
Input: root = [1]
21+
Output: [1]
22+
23+
Example 4:
24+
Input: root = [1,2]
25+
Output: [2,1]
26+
27+
Example 5:
28+
Input: root = [1,null,2]
29+
Output: [1,2]
30+
31+
Constraints:
32+
The number of nodes in the tree is in the range [0, 100].
33+
-100 <= Node.val <= 100
34+
*/
35+
36+
public List<Integer> visited;
37+
public Solution() {
38+
this.visited = new ArrayList<>();
39+
}
40+
41+
public List<Integer> inorderTraversal(TreeNode root) {
42+
if (root == null) {
43+
return null;
44+
}
45+
inorderTraversal(root.left);
46+
visited.add(root.val);
47+
inorderTraversal(root.right);
48+
return visited;
49+
}
50+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package leetInorder;
2+
3+
// from leetcode
4+
public class TreeNode {
5+
6+
public int val;
7+
public TreeNode left;
8+
public TreeNode right;
9+
public TreeNode() {}
10+
public TreeNode(int val) {
11+
this.val = val;
12+
left = right = null;
13+
}
14+
public TreeNode(int val, TreeNode left, TreeNode right) {
15+
this.val = val;
16+
this.left = left;
17+
this.right = right;
18+
}
19+
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package leetInorder;
2+
3+
import org.junit.Test;
4+
import java.util.List;
5+
6+
import static org.junit.Assert.assertTrue;
7+
8+
import java.util.ArrayList;
9+
10+
public class SolutionTests {
11+
12+
public static boolean traversedOK(List<Integer> observed,
13+
List<Integer> expected) {
14+
for (int i = 0; i < expected.size(); i++) {
15+
if (observed.get(i) - expected.get(i) != 0) {
16+
return false;
17+
}
18+
i++;
19+
}
20+
return true;
21+
}
22+
23+
public static List<Integer> getObserved(TreeNode aRoot) {
24+
Solution sol = new Solution();
25+
return sol.inorderTraversal(aRoot);
26+
}
27+
28+
@Test
29+
public void test_1_2_returns_2_1() {
30+
TreeNode aRoot = new TreeNode(1);
31+
aRoot.left = new TreeNode(2);
32+
List<Integer> expected = new ArrayList<>();
33+
expected.add(2); expected.add(1);
34+
assertTrue(traversedOK(getObserved(aRoot), expected));
35+
}
36+
37+
@Test
38+
public void test_1_returns_1() {
39+
TreeNode aRoot = new TreeNode(1);
40+
List<Integer> expected = new ArrayList<>();
41+
expected.add(0, 1);
42+
assertTrue(traversedOK(getObserved(aRoot), expected));
43+
}
44+
45+
@Test
46+
public void test_1_null_2_3_returns_1_3_2() {
47+
List<Integer> expected = new ArrayList<>();
48+
expected.add(0, 1); expected.add(1, 3); expected.add(2, 2);
49+
List<TreeNode> nodes = new ArrayList<>();
50+
for (int i = 1; i < 4; i++) {
51+
nodes.add(new TreeNode(i));
52+
}
53+
nodes.get(0).right = nodes.get(1);
54+
nodes.get(1).left = nodes.get(2);
55+
assertTrue(traversedOK(getObserved(nodes.get(0)), expected));
56+
}
57+
58+
@Test
59+
public void test_1_null_2_returns_1_2() {
60+
List<Integer> expected = new ArrayList<>();
61+
expected.add(1); expected.add(2);
62+
TreeNode aRoot = new TreeNode(1);
63+
aRoot.right = new TreeNode(2);
64+
assertTrue(traversedOK(getObserved(aRoot), expected));
65+
}
66+
67+
}

0 commit comments

Comments
 (0)