Skip to content

Commit 9069e7b

Browse files
committed
init commit
0 parents  commit 9069e7b

File tree

17 files changed

+804
-0
lines changed

17 files changed

+804
-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: 133 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: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
# java_binary_tree_maximum_path_sum
2+
3+
**path** in a binary tree is a sequence of nodes where each pair of adjacent nodes in the sequence has an edge connecting them. A node can only appear in the sequence **at most once**. Note that the path does not need to pass through the root.
4+
5+
The **path sum** of a path is the sum of the node's values in the path.
6+
7+
Given the `root` of a binary tree, return *the maximum **path sum** of any **non-empty** path*.
8+
9+
## Examples
10+
11+
**Example 1:**
12+
13+
![https://assets.leetcode.com/uploads/2020/10/13/exx1.jpg](https://assets.leetcode.com/uploads/2020/10/13/exx1.jpg)
14+
15+
```
16+
Input: root = [1,2,3]
17+
Output: 6
18+
Explanation: The optimal path is 2 -> 1 -> 3 with a path sum of 2 + 1 + 3 = 6.
19+
20+
```
21+
22+
**Example 2:**
23+
24+
![https://assets.leetcode.com/uploads/2020/10/13/exx2.jpg](https://assets.leetcode.com/uploads/2020/10/13/exx2.jpg)
25+
26+
```
27+
Input: root = [-10,9,20,null,null,15,7]
28+
Output: 42
29+
Explanation: The optimal path is 15 -> 20 -> 7 with a path sum of 15 + 20 + 7 = 42.
30+
31+
```
32+
33+
**Constraints:**
34+
35+
- The number of nodes in the tree is in the range $`[1, 3*10^4]`$.
36+
- `1000 <= Node.val <= 1000`
37+
38+
## 解析
39+
40+
題目給了一個二元樹根結點 root
41+
42+
定義一個 path 的 sum 代表把在 path 上所有的結點值相加
43+
44+
找出一個樹的 path 所能形成最大 sum,且每個結點在路徑只能出現一次
45+
46+
這個問題的核心在於要如何找出最大值
47+
48+
透過累計的方式我們可以從 root 結點來分析
49+
50+
從 root 拆解出, 從root 結點分岔 + 從 root 點不分岔兩種包含 root 結點方式
51+
52+
而這個問題,可以用DFS 來做探訊
53+
54+
假設不做分岔的方法是 maxSum
55+
56+
累計量 accum = max(accum, root.Val + Max(maxSum(root.left) , maxSum(root.Right))
57+
58+
對包含 root 的樹去找 maxSum(root) = root.Val + Max(maxSum(root.left) , maxSum(root.Right) )
59+
60+
這樣只要走訪完整棵樹即為最大值, 時間複雜度O(n)
61+
62+
參考下圖
63+
64+
![](https://i.imgur.com/Hapsjw0.png)
65+
66+
## 程式碼
67+
```java
68+
class Solution {
69+
/**
70+
* Definition for a binary tree node.
71+
* public class TreeNode {
72+
* int val;
73+
* TreeNode left;
74+
* TreeNode right;
75+
* TreeNode() {}
76+
* TreeNode(int val) { this.val = val; }
77+
* TreeNode(int val, TreeNode left, TreeNode right) {
78+
* this.val = val;
79+
* this.left = left;
80+
* this.right = right;
81+
* }
82+
* }
83+
*/
84+
public int maxPathSum(TreeNode root) {
85+
if (root == null) {
86+
return 0;
87+
}
88+
accumResult = root.val;
89+
MaxSum(root);
90+
return accumResult;
91+
}
92+
public int MaxSum(TreeNode root) {
93+
if (root == null) {
94+
return 0;
95+
}
96+
int leftMax = MaxSum(root.left);
97+
int rightMax = MaxSum(root.right);
98+
// leftMax = choose or not choose maximum
99+
leftMax = Math.max(leftMax, 0);
100+
rightMax = Math.max(rightMax, 0);
101+
// split Max = not split result or (leftMax + root.val + rightMax)
102+
accumResult = Math.max(accumResult, leftMax + root.val + rightMax);
103+
// not split result = root.val + Max(leftMax, rightMax)
104+
return root.val + Math.max(leftMax, rightMax);
105+
}
106+
}
107+
```
108+
## 困難點
109+
110+
1. Understand DFS
111+
2. Know how to divide the question
112+
113+
## Solve Point
114+
115+
- [x] Understand what problem to solve
116+
- [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.

0 commit comments

Comments
 (0)