Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit bf14a68

Browse files
committed
Merge pull request #370 from 0xff-dev/1026
Add solution and test-cases for problem 1026
2 parents 05a3ab8 + fed09d2 commit bf14a68

File tree

5 files changed

+117
-24
lines changed

5 files changed

+117
-24
lines changed

‎leetcode/1001-1100/1026.Maximum-Difference-Between-Node-and-Ancestor/README.md

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,31 @@
11
# [1026.Maximum Difference Between Node and Ancestor][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
4+
Given the `root` of a binary tree, find the maximum value `v` for which there exist **different** nodes `a` and `b` where `v = |a.val - b.val|` and `a` is an ancestor of `b`.
5+
6+
A node `a` is an ancestor of `b` if either: any child of `a` is equal to `b` any child of `a` is an ancestor of `b`.
77

8-
**Example 1:**
8+
**Example 1:**
9+
![example1](./tmp-tree.jpg)
910

1011
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
12+
Input: root = [8,3,10,1,6,null,14,null,null,4,7,13]
13+
Output: 7
14+
Explanation: We have various ancestor-node differences, some of which are given below :
15+
|8 - 3| = 5
16+
|3 - 7| = 4
17+
|8 - 1| = 7
18+
|10 - 13| = 3
19+
Among all possible differences, the maximum value of 7 is obtained by |8 - 1| = 7.
1320
```
1421

15-
## 题意
16-
> ...
17-
18-
## 题解
22+
**Example 2:**
23+
![example2](./tmp-tree-1.jpg)
1924

20-
### 思路1
21-
> ...
22-
Maximum Difference Between Node and Ancestor
23-
```go
2425
```
25-
26+
Input: root = [1,null,2,null,0,3]
27+
Output: 3
28+
```
2629

2730
## 结语
2831

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,50 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
type TreeNode struct {
4+
Val int
5+
Left, Right *TreeNode
6+
}
7+
8+
func Solution(root *TreeNode) int {
9+
if root == nil {
10+
return 0
11+
}
12+
ans := -1
13+
maxAncestorDiff1026(root, &ans)
14+
return ans
15+
}
16+
17+
func check1026(ans *int, others []int) (int, int) {
18+
root, m, n := others[0], others[0], others[0]
19+
for idx := 1; idx < len(others); idx++ {
20+
diff := others[idx] - root
21+
if diff < 0 {
22+
diff = -diff
23+
}
24+
if *ans == -1 || diff > *ans {
25+
*ans = diff
26+
}
27+
if others[idx] > m {
28+
m = others[idx]
29+
}
30+
if others[idx] < n {
31+
n = others[idx]
32+
}
33+
}
34+
return m, n
35+
}
36+
func maxAncestorDiff1026(root *TreeNode, ans *int) (int, int) {
37+
other := []int{root.Val}
38+
if root.Left != nil {
39+
40+
leftMax, leftMin := maxAncestorDiff1026(root.Left, ans)
41+
other = append(other, leftMax, leftMin)
42+
}
43+
44+
if root.Right != nil {
45+
rightMax, rightMin := maxAncestorDiff1026(root.Right, ans)
46+
other = append(other, rightMax, rightMin)
47+
}
48+
49+
return check1026(ans, other)
550
}

‎leetcode/1001-1100/1026.Maximum-Difference-Between-Node-and-Ancestor/Solution_test.go

Lines changed: 52 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,57 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
inputs *TreeNode
14+
expect int
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", &TreeNode{
17+
Val: 8,
18+
Left: &TreeNode{
19+
Val: 3,
20+
Left: &TreeNode{Val: 1},
21+
Right: &TreeNode{
22+
Val: 6,
23+
Left: &TreeNode{Val: 4},
24+
Right: &TreeNode{Val: 7},
25+
},
26+
},
27+
Right: &TreeNode{
28+
Val: 10,
29+
Right: &TreeNode{
30+
Val: 14,
31+
Left: &TreeNode{
32+
Val: 13,
33+
},
34+
},
35+
},
36+
}, 7},
37+
{"TestCase2", &TreeNode{
38+
Val: 1,
39+
Right: &TreeNode{
40+
Val: 2,
41+
Right: &TreeNode{
42+
Val: 0,
43+
Left: &TreeNode{
44+
Val: 3,
45+
},
46+
},
47+
},
48+
}, 3},
49+
{"TestCase3", &TreeNode{
50+
Val: 2,
51+
Right: &TreeNode{
52+
Val: 0,
53+
Right: &TreeNode{
54+
Val: 4,
55+
Right: &TreeNode{
56+
Val: 3,
57+
Right: &TreeNode{
58+
Val: 1,
59+
},
60+
},
61+
},
62+
},
63+
}, 4},
1964
}
2065

2166
// 开始测试
@@ -30,10 +75,10 @@ func TestSolution(t *testing.T) {
3075
}
3176
}
3277

33-
//压力测试
78+
//压力测试
3479
func BenchmarkSolution(b *testing.B) {
3580
}
3681

37-
//使用案列
82+
//使用案列
3883
func ExampleSolution() {
3984
}
9.41 KB
Loading[フレーム]
20.2 KB
Loading[フレーム]

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /