Skip to content

Commit 500f600

Browse files
committed
init commit
0 parents  commit 500f600

File tree

17 files changed

+809
-0
lines changed

17 files changed

+809
-0
lines changed

.github/workflows/java.yml

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
name: Java
2+
3+
on:
4+
push:
5+
branches:
6+
- master
7+
pull_request:
8+
branches:
9+
- master
10+
11+
jobs:
12+
build:
13+
runs-on: ubuntu-latest
14+
steps:
15+
- name: Checkout
16+
uses: actions/checkout@v3
17+
- name: Setup JDK 11
18+
uses: actions/setup-java@v3
19+
with:
20+
distribution: 'adopt'
21+
java-version: '11'
22+
- name: Grant execution permission for gradlew
23+
run: chmod +x gradlew
24+
- name: Cache Gradle packages
25+
uses: actions/cache@v3
26+
with:
27+
path: ~/.gradle/caches
28+
key: ${{ runner.os }}-gradle-${{ github.event.repository.name }}-${{ hashFiles('**/*.gradle')}}
29+
restore-keys: ${{ runner.os }}-gradle-${{ github.event.repository.name }}
30+
- name: Build with Gradlew
31+
run: ./gradlew build
32+
- name: Test with Gradlew
33+
run: ./gradlew :test --tests "SolutionTest"

.gitignore

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
.gradle
2+
build/
3+
!gradle/wrapper/gradle-wrapper.jar
4+
!**/src/main/**/build/
5+
!**/src/test/**/build/
6+
7+
### IntelliJ IDEA ###
8+
.idea/modules.xml
9+
.idea/jarRepositories.xml
10+
.idea/compiler.xml
11+
.idea/libraries/
12+
*.iws
13+
*.iml
14+
*.ipr
15+
out/
16+
!**/src/main/**/out/
17+
!**/src/test/**/out/
18+
19+
### Eclipse ###
20+
.apt_generated
21+
.classpath
22+
.factorypath
23+
.project
24+
.settings
25+
.springBeans
26+
.sts4-cache
27+
bin/
28+
!**/src/main/**/bin/
29+
!**/src/test/**/bin/
30+
31+
### NetBeans ###
32+
/nbproject/private/
33+
/nbbuild/
34+
/dist/
35+
/nbdist/
36+
/.nb-gradle/
37+
38+
### VS Code ###
39+
.vscode/
40+
41+
### Mac OS ###
42+
.DS_Store

.husky/pre-commit

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#!/bin/bash
2+
./gradlew :test --tests "SolutionTest"

.idea/gradle.xml

Lines changed: 16 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/misc.xml

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/vcs.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/workspace.xml

Lines changed: 128 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
# java_serialize_deserialize_binary_tree
2+
3+
Serialization is the process of converting a data structure or object into a sequence of bits so that it can be stored in a file or memory buffer, or transmitted across a network connection link to be reconstructed later in the same or another computer environment.
4+
5+
Design an algorithm to serialize and deserialize a binary tree. There is no restriction on how your serialization/deserialization algorithm should work. You just need to ensure that a binary tree can be serialized to a string and this string can be deserialized to the original tree structure.
6+
7+
**Clarification:** The input/output format is the same as [how LeetCode serializes a binary tree](https://leetcode.com/faq/#binary-tree). You do not necessarily need to follow this format, so please be creative and come up with different approaches yourself.
8+
9+
## Examples
10+
11+
**Example 1:**
12+
13+
![https://assets.leetcode.com/uploads/2020/09/15/serdeser.jpg](https://assets.leetcode.com/uploads/2020/09/15/serdeser.jpg)
14+
15+
```
16+
Input: root = [1,2,3,null,null,4,5]
17+
Output: [1,2,3,null,null,4,5]
18+
19+
```
20+
21+
**Example 2:**
22+
23+
```
24+
Input: root = []
25+
Output: []
26+
27+
```
28+
29+
**Constraints:**
30+
31+
- The number of nodes in the tree is in the range `[0, 104]`.
32+
- `1000 <= Node.val <= 1000`
33+
34+
## 解析
35+
36+
給定一個二元樹結點 root 要求要實作 serial, deserial 方法
37+
38+
可以透過 DFS 實作 Pre-order format
39+
40+
如下圖
41+
42+
![](https://i.imgur.com/rMQXbOx.png)
43+
44+
## 程式碼
45+
```java
46+
class Solution {
47+
// Encodes a tree to a single string.
48+
public String serialize(TreeNode root) {
49+
List<String> list = new ArrayList<>();
50+
DFS(root, list);
51+
return String.join(",", list);
52+
}
53+
public void DFS(TreeNode root, List<String> list) {
54+
if (root == null) {
55+
list.add("N");
56+
return;
57+
}
58+
list.add(String.valueOf(root.val));
59+
DFS(root.left, list);
60+
DFS(root.right, list);
61+
}
62+
// Decodes your encoded data to tree.
63+
int count;
64+
public TreeNode deserialize(String data) {
65+
String[] list = data.split(",");
66+
count = 0;
67+
return DE_DFS(list);
68+
}
69+
public TreeNode DE_DFS(String[] list) {
70+
if (list[count].equals("N")){
71+
count++;
72+
return null;
73+
}
74+
int rootValue = Integer.parseInt(list[count]);
75+
count++;
76+
TreeNode root = new TreeNode(rootValue);
77+
root.left = DE_DFS(list);
78+
root.right = DE_DFS(list);
79+
return root;
80+
}
81+
}
82+
```
83+
## 困難點
84+
85+
1. Understand DFS
86+
87+
## Solve Point
88+
89+
- [x] Understand what problem need to solve
90+
- [x] Analysis Complexity

build.gradle

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
plugins {
2+
id 'java'
3+
}
4+
5+
group 'org.example'
6+
version '1.0-SNAPSHOT'
7+
8+
repositories {
9+
mavenCentral()
10+
}
11+
12+
dependencies {
13+
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.1'
14+
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.1'
15+
}
16+
17+
test {
18+
useJUnitPlatform()
19+
}

gradle/wrapper/gradle-wrapper.jar

58.4 KB
Binary file not shown.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
distributionBase=GRADLE_USER_HOME
2+
distributionPath=wrapper/dists
3+
distributionUrl=https\://services.gradle.org/distributions/gradle-7.4-bin.zip
4+
zipStoreBase=GRADLE_USER_HOME
5+
zipStorePath=wrapper/dists

0 commit comments

Comments
 (0)