Skip to content

Commit b56aa97

Browse files
committed
init commit
0 parents  commit b56aa97

File tree

17 files changed

+805
-0
lines changed

17 files changed

+805
-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: 130 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: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
# java_valid_BST
2+
3+
Given the `root` of a binary tree, *determine if it is a valid binary search tree (BST)*.
4+
5+
**valid BST** is defined as follows:
6+
7+
- The left subtree of a node contains only nodes with keys **less than** the node's key.
8+
- The right subtree of a node contains only nodes with keys **greater than** the node's key.
9+
- Both the left and right subtrees must also be binary search trees.
10+
11+
## Examples
12+
13+
**Example 1:**
14+
15+
![https://assets.leetcode.com/uploads/2020/12/01/tree1.jpg](https://assets.leetcode.com/uploads/2020/12/01/tree1.jpg)
16+
17+
```
18+
Input: root = [2,1,3]
19+
Output: true
20+
21+
```
22+
23+
**Example 2:**
24+
25+
![https://assets.leetcode.com/uploads/2020/12/01/tree2.jpg](https://assets.leetcode.com/uploads/2020/12/01/tree2.jpg)
26+
27+
```
28+
Input: root = [5,1,4,null,null,3,6]
29+
Output: false
30+
Explanation: The root node's value is 5 but its right child's value is 4.
31+
32+
```
33+
34+
**Constraints:**
35+
36+
- The number of nodes in the tree is in the range `[1, 104]`.
37+
- `231 <= Node.val <= 231 - 1`
38+
39+
## 解析
40+
41+
題目給定一個二元樹的根結點 root
42+
43+
想要判斷這個由 root 所形成的樹是不是一棵二元搜尋樹
44+
45+
如果 root 所形成的樹是一個二元搜尋樹有以下特性
46+
47+
1. root.Left 所形成的樹內所有結點值都小於 root.Val
48+
2. root.Right 所形成的樹內所有結點值都大於 root.Val
49+
3. root.Left 與 root.Right 所形成的樹都是二元搜尋樹
50+
51+
最直覺的想法
52+
53+
每次遇到一個結點
54+
55+
檢查這個結點的左子樹有沒有都小於這個 root 結點
56+
57+
檢查這個結點的右子樹有沒有都大於這個 root 結點
58+
59+
這樣每次都要走訪 n個 所以時間複雜度 是 O($n^2$)
60+
61+
如果不想要每次都要走訪所有走過的結點則必須思考以下特性
62+
63+
對一個二元搜尋樹的某個結點 node
64+
65+
node.left 所形成樹的上界是 node.Val ,因為所有 node.Left 所形成結點都必須小於 node.Val
66+
67+
所以對於 node.Left 所形成樹 只要檢查是否都有小於 node.Val 即可
68+
69+
同樣的,node.Right 所形成樹的下界是 node.Val ,因為所有 node.Right 所形成結點都必須大於 node.Val
70+
71+
所以對於 node.Right 所形成樹 只要檢查是否都有大於 node.Val 即可
72+
73+
![](https://i.imgur.com/gM2lJ9d.png)
74+
75+
一開始的根結點 沒有上下界相當於 上界是 infinity , 下界是 -infinity
76+
77+
每次只要檢查目前結點有沒有介於上下界
78+
79+
每次往左結點走更新上界是目前結點
80+
81+
每次往右結點走更新下界是目前結點
82+
83+
## 程式碼
84+
85+
```java
86+
class Solution {
87+
/**
88+
* Definition for a binary tree node.
89+
* public class TreeNode {
90+
* int val;
91+
* TreeNode left;
92+
* TreeNode right;
93+
* TreeNode() {}
94+
* TreeNode(int val) { this.val = val; }
95+
* TreeNode(int val, TreeNode left, TreeNode right) {
96+
* this.val = val;
97+
* this.left = left;
98+
* this.right = right;
99+
* }
100+
* }
101+
*/
102+
public boolean isValidBST(TreeNode root) {
103+
if (root == null) {
104+
return true;
105+
}
106+
return checkBST(root, Double.MAX_VALUE, -Double.MAX_VALUE);
107+
}
108+
public boolean checkBST(TreeNode root, double upperbound, double lowerbound) {
109+
if (root == null) {
110+
return true;
111+
}
112+
double cur = (double)(root.val);
113+
if (cur >= upperbound || cur <= lowerbound) {
114+
return false;
115+
}
116+
return checkBST(root.left, cur, lowerbound) && checkBST(root.right, upperbound, cur);
117+
}
118+
}
119+
```
120+
121+
## 困難點
122+
123+
1. 理解二元搜尋樹的定義
124+
2. 透過理解二元搜尋樹的定義來減少檢查的範圍
125+
126+
## Solve Point
127+
128+
- [x] Understand what problem need to be solve
129+
- [x] Analysis Complexity

0 commit comments

Comments
 (0)