Skip to content

Commit 28aa316

Browse files
authored
Merge pull request #915 from 0xff-dev/2096
Add solution and test-cases for problem 2096
2 parents 6e5fc68 + 863c479 commit 28aa316

File tree

5 files changed

+90
-27
lines changed

5 files changed

+90
-27
lines changed

leetcode/2001-2100/2096.Step-By-Step-Directions-From-a-Binary-Tree-Node-to-Another/README.md

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,35 @@
11
# [2096.Step-By-Step Directions From a Binary Tree Node to Another][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 are given the `root` of a **binary tree** with `n` nodes. Each node is uniquely assigned a value from `1` to `n`. You are also given an integer `startValue` representing the value of the start node `s`, and a different integer `destValue` representing the value of the destination node `t`.
5+
6+
Find the **shortest path** starting from node `s` and ending at node `t`. Generate step-by-step directions of such path as a string consisting of only the **uppercase** letters `'L'`, `'R'`, and `'U'`. Each letter indicates a specific direction:
7+
8+
- `'L'` means to go from a node to its **left child** node.
9+
- `'R'` means to go from a node to its **right child** node.
10+
- `'U`' means to go from a node to its **parent** node.
11+
12+
Return the step-by-step directions of the **shortest path** from node `s` to node `t`.
713

8-
**Example 1:**
14+
**Example 1:**
15+
16+
![1](./eg1.png)
917

1018
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
19+
Input: root = [5,1,2,3,null,6,4], startValue = 3, destValue = 6
20+
Output: "UURL"
21+
Explanation: The shortest path is: 3 → 1 → 5 → 2 → 6.
1322
```
1423

15-
## 题意
16-
> ...
24+
**Example 2:**
1725

18-
## 题解
26+
![2](./eg2.png)
1927

20-
### 思路1
21-
> ...
22-
Step-By-Step Directions From a Binary Tree Node to Another
23-
```go
2428
```
25-
29+
Input: root = [2,1], startValue = 2, destValue = 1
30+
Output: "L"
31+
Explanation: The shortest path is: 2 → 1.
32+
```
2633

2734
## 结语
2835

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, startValue int, destValue int) string {
9+
var dfs func(*TreeNode, *[]int, *[]byte, int) bool
10+
dfs = func(tree *TreeNode, nums *[]int, dirs *[]byte, value int) bool {
11+
if tree == nil {
12+
return false
13+
}
14+
if tree.Val == value {
15+
return true
16+
}
17+
if tree.Left != nil {
18+
*nums = append(*nums, tree.Left.Val)
19+
*dirs = append(*dirs, 'L')
20+
if dfs(tree.Left, nums, dirs, value) {
21+
return true
22+
}
23+
*nums = (*nums)[:len(*nums)-1]
24+
*dirs = (*dirs)[:len(*dirs)-1]
25+
26+
}
27+
if tree.Right != nil {
28+
*nums = append(*nums, tree.Right.Val)
29+
*dirs = append(*dirs, 'R')
30+
if dfs(tree.Right, nums, dirs, value) {
31+
return true
32+
}
33+
*nums = (*nums)[:len(*nums)-1]
34+
*dirs = (*dirs)[:len(*dirs)-1]
35+
}
36+
return false
37+
}
38+
nums1, nums2 := []int{}, []int{}
39+
dirs1, dirs2 := []byte{}, []byte{}
40+
_ = dfs(root, &nums1, &dirs1, startValue)
41+
_ = dfs(root, &nums2, &dirs2, destValue)
42+
43+
i := 0
44+
for ; i < len(nums1) && i < len(nums2) && nums1[i] == nums2[i]; i++ {
45+
}
46+
for i1 := i; i1 < len(dirs1); i1++ {
47+
dirs1[i1] = 'U'
48+
}
49+
return string(dirs1[i:]) + string(dirs2[i:])
550
}

leetcode/2001-2100/2096.Step-By-Step-Directions-From-a-Binary-Tree-Node-to-Another/Solution_test.go

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,31 +9,42 @@ import (
99
func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
12-
name string
13-
inputs bool
14-
expect bool
12+
name string
13+
tree *TreeNode
14+
start, dest int
15+
expect string
1516
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
17+
{"TestCase1", &TreeNode{
18+
Val: 5,
19+
Left: &TreeNode{
20+
Val: 1,
21+
Left: &TreeNode{Val: 3},
22+
},
23+
Right: &TreeNode{
24+
Val: 2,
25+
Left: &TreeNode{Val: 6},
26+
Right: &TreeNode{Val: 4},
27+
},
28+
}, 3, 6, "UURL"},
29+
{"TestCase2", &TreeNode{Val: 2, Left: &TreeNode{Val: 1}}, 2, 1, "L"},
1930
}
2031

2132
// 开始测试
2233
for i, c := range cases {
2334
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
35+
got := Solution(c.tree, c.start, c.dest)
2536
if !reflect.DeepEqual(got, c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, got, c.inputs)
37+
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v %v",
38+
c.expect, got, c.tree, c.start, c.dest)
2839
}
2940
})
3041
}
3142
}
3243

33-
// 压力测试
44+
// 压力测试
3445
func BenchmarkSolution(b *testing.B) {
3546
}
3647

37-
// 使用案列
48+
// 使用案列
3849
func ExampleSolution() {
3950
}
Loading
Loading

0 commit comments

Comments
 (0)