Skip to content

Commit 5af5d7e

Browse files
authored
Merge pull request 6boris#427 from 0xff-dev/1361
Add solution and test-cases for problem 1361
2 parents 3871f0a + b5e97bc commit 5af5d7e

File tree

6 files changed

+87
-26
lines changed

6 files changed

+87
-26
lines changed
7.1 KB
Loading
8.32 KB
Loading
3.38 KB
Loading

leetcode/1301-1400/1361.Validate-Binary-Tree-Nodes/README.md

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,39 @@
11
# [1361.Validate Binary Tree Nodes][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+
You have `n` binary tree nodes numbered from `0` to `n - 1` where node `i` has two children `leftChild[i]` and `rightChild[i]`, return `true` if and only if **all** the given nodes form **exactly one** valid binary tree.
5+
6+
If node `i` has no left child then `leftChild[i]` will equal `-1`, similarly for the right child.
7+
8+
Note that the nodes have no values and that we only use the node numbers in this problem.
79

8-
**Example 1:**
10+
**Example 1:**
11+
12+
![example1](./1503_ex1.png)
913

1014
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
15+
Input: n = 4, leftChild = [1,-1,3,-1], rightChild = [2,-1,-1,-1]
16+
Output: true
1317
```
1418

15-
## 题意
16-
> ...
19+
**Example 2:**
1720

18-
## 题解
21+
![example2](./1503_ex2.png)
1922

20-
### 思路1
21-
> ...
22-
Validate Binary Tree Nodes
23-
```go
23+
```
24+
Input: n = 4, leftChild = [1,-1,3,-1], rightChild = [2,3,-1,-1]
25+
Output: false
2426
```
2527

28+
**Example 3:**
29+
30+
![example3](./1503_ex3.png)
31+
32+
33+
```
34+
Input: n = 2, leftChild = [1,0], rightChild = [-1,-1]
35+
Output: false
36+
```
2637

2738
## 结语
2839

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

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(n int, leftChild []int, rightChild []int) bool {
4+
5+
father := make([]int, n)
6+
for i := 0; i < n; i++ {
7+
// 孤立的节点
8+
father[i] = i
9+
}
10+
11+
for node := 0; node < n; node++ {
12+
left := leftChild[node]
13+
if left != -1 {
14+
lf := father[left]
15+
if lf != left && lf != node || left == father[node] {
16+
return false
17+
}
18+
father[left] = node
19+
}
20+
right := rightChild[node]
21+
if right != -1 {
22+
rf := father[right]
23+
if rf != right && rf != node || right == father[node] {
24+
return false
25+
}
26+
father[right] = node
27+
}
28+
}
29+
/*
30+
0
31+
1 2
32+
0 1 2
33+
*/
34+
sameFather := -1
35+
for node := 0; node < n; node++ {
36+
steps := 0
37+
walkerNode := node
38+
nodeFather := father[walkerNode]
39+
for nodeFather != walkerNode && steps <= n {
40+
walkerNode = nodeFather
41+
nodeFather = father[walkerNode]
42+
steps++
43+
}
44+
if steps > n {
45+
return false
46+
}
47+
if sameFather != -1 && nodeFather != sameFather {
48+
return false
49+
}
50+
sameFather = nodeFather
51+
}
52+
53+
return true
554
}

leetcode/1301-1400/1361.Validate-Binary-Tree-Nodes/Solution_test.go

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,31 +9,32 @@ import (
99
func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
12-
name string
13-
inputs bool
14-
expect bool
12+
name string
13+
n int
14+
left, right []int
15+
expect bool
1516
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
17+
{"TestCase1", 4, []int{1, -1, 3, -1}, []int{2, -1, -1, -1}, true},
18+
{"TestCase2", 4, []int{1, -1, 3, -1}, []int{2, 3, -1, -1}, false},
19+
{"TestCase3", 2, []int{1, 0}, []int{-1, -1}, false},
1920
}
2021

2122
// 开始测试
2223
for i, c := range cases {
2324
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
25+
got := Solution(c.n, c.left, c.right)
2526
if !reflect.DeepEqual(got, c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, got, c.inputs)
27+
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v %v",
28+
c.expect, got, c.n, c.left, c.right)
2829
}
2930
})
3031
}
3132
}
3233

33-
// 压力测试
34+
// 压力测试
3435
func BenchmarkSolution(b *testing.B) {
3536
}
3637

37-
// 使用案列
38+
// 使用案列
3839
func ExampleSolution() {
3940
}

0 commit comments

Comments
 (0)