Skip to content

Commit 4aa94e2

Browse files
committed
add some new solution for 563
1 parent c3ab7ec commit 4aa94e2

File tree

7 files changed

+114
-22
lines changed

7 files changed

+114
-22
lines changed

leetcode/501-600/0563.Binary-Tree-Tilt/README.md

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,45 @@
11
# [563.Binary Tree Tilt][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, return the sum of every tree node's __tilt__.
75

8-
**Example 1:**
6+
The __tilt__ of a tree node is the __absolute difference__ between the sum of all left subtree node __values__ and all right subtree node __values__. If a node does not have a left child, then the sum of the left subtree node __values__ is treated as `0`. The rule is similar if there the node does not have a right child.
97

10-
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
13-
```
148

15-
## 题意
16-
> ...
9+
**Example 1:**
10+
![tilt1](./tilt1.jpeg)
1711

18-
## 题解
12+
```
13+
Input: root = [1,2,3]
14+
Output: 1
15+
Explanation:
16+
Tilt of node 2 : |0-0| = 0 (no children)
17+
Tilt of node 3 : |0-0| = 0 (no children)
18+
Tilt of node 1 : |2-3| = 1 (left subtree is just left child, so sum is 2; right subtree is just right child, so sum is 3)
19+
Sum of every tilt : 0 + 0 + 1 = 1
20+
```
1921

20-
### 思路1
21-
> ...
22-
Binary Tree Tilt
23-
```go
22+
**Example 2:**
23+
![tilt2](./tilt2.jpeg)
24+
```
25+
Input: root = [4,2,9,3,5,null,7]
26+
Output: 15
27+
Explanation:
28+
Tilt of node 3 : |0-0| = 0 (no children)
29+
Tilt of node 5 : |0-0| = 0 (no children)
30+
Tilt of node 7 : |0-0| = 0 (no children)
31+
Tilt of node 2 : |3-5| = 2 (left subtree is just left child, so sum is 3; right subtree is just right child, so sum is 5)
32+
Tilt of node 9 : |0-7| = 7 (no left child, so sum is 0; right subtree is just right child, so sum is 7)
33+
Tilt of node 4 : |(3+5+2)-(9+7)| = |10-16| = 6 (left subtree values are 3, 5, and 2, which sums to 10; right subtree values are 9 and 7, which sums to 16)
34+
Sum of every tilt : 0 + 0 + 0 + 2 + 7 + 6 = 15
2435
```
2536

37+
**Example 3:**
38+
![tilt3](./tilt3.jpeg)
39+
```
40+
Input: root = [21,7,14,1,1,2,2,3,3]
41+
Output: 9
42+
```
2643

2744
## 结语
2845

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

3-
func Solution(x bool) bool {
4-
return x
3+
func findTiltAux(root *TreeNode, tilt *int) int {
4+
if root == nil {
5+
return 0
6+
}
7+
8+
left := findTiltAux(root.Left, tilt)
9+
right := findTiltAux(root.Right, tilt)
10+
abs := left - right
11+
if abs < 0 {
12+
abs = -abs
13+
}
14+
*tilt += abs
15+
return left + right + root.Val
16+
}
17+
18+
func Solution(root *TreeNode) int {
19+
tilt := 0
20+
_ = findTiltAux(root, &tilt)
21+
return tilt
522
}

leetcode/501-600/0563.Binary-Tree-Tilt/Solution_test.go

Lines changed: 56 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,63 @@ 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: 1,
18+
Left: &TreeNode{
19+
Val: 2,
20+
},
21+
Right: &TreeNode{
22+
Val: 3,
23+
},
24+
}, 1},
25+
{"TestCase", &TreeNode{
26+
Val: 4,
27+
Left: &TreeNode{
28+
Val: 2,
29+
Left: &TreeNode{
30+
Val: 3,
31+
},
32+
Right: &TreeNode{
33+
Val: 5,
34+
},
35+
},
36+
Right: &TreeNode{
37+
Val: 9,
38+
Right: &TreeNode{
39+
Val: 7,
40+
},
41+
},
42+
}, 15},
43+
{"TestCase", &TreeNode{
44+
Val: 21,
45+
Left: &TreeNode{
46+
Val: 7,
47+
Left: &TreeNode{
48+
Val: 1,
49+
Left: &TreeNode{
50+
Val: 3,
51+
},
52+
Right: &TreeNode{
53+
Val: 3,
54+
},
55+
},
56+
Right: &TreeNode{
57+
Val: 1,
58+
},
59+
},
60+
Right: &TreeNode{
61+
Val: 14,
62+
Left: &TreeNode{
63+
Val: 2,
64+
},
65+
Right: &TreeNode{
66+
Val: 2,
67+
},
68+
},
69+
}, 9},
1970
}
2071

2172
// 开始测试
14.4 KB
Loading
31.7 KB
Loading
44.2 KB
Loading
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package Solution
2+
3+
type TreeNode struct {
4+
Val int
5+
Left *TreeNode
6+
Right *TreeNode
7+
}

0 commit comments

Comments
 (0)