Skip to content

Commit 798d97f

Browse files
committed
Add linked-list-in-binary-tree
1 parent 99b8050 commit 798d97f

File tree

8 files changed

+155
-4
lines changed

8 files changed

+155
-4
lines changed

algorithms/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
|0148|[排序链表](https://leetcode-cn.com/problems/sort-list/)|[go](./linkedList/148.sortList.go)|M|
4141
|0155|[最小栈](https://leetcode-cn.com/problems/min-stack/)|[go](./mystack/155.minStack.go)|S|
4242
|0171|[Excel表列序号](https://leetcode-cn.com/problems/excel-sheet-column-number/)|[go](./mystring/171.titleToNumber.go)|S|
43-
|0173|[二叉搜索树迭代器](https://leetcode-cn.com/problems/binary-search-tree-iterator/)|[go](./mystack/173.BSTIterator.go)|M|
43+
|0173|[二叉搜索树迭代器](https://leetcode-cn.com/problems/binary-search-tree-iterator/)|[go](./tree/173.BSTIterator.go) [go](./mystack/173.BSTIterator.go)|M|
4444
|0206|[反转链表](https://leetcode-cn.com/problems/reverse-linked-list/)|[go](./linkedList/206.reverseList.go)|S|
4545
|0208|[实现 Trie (前缀树)](https://leetcode-cn.com/problems/implement-trie-prefix-tree/)|[go](./trie/208.trie.go)|M|
4646
|0215|[数组中的第K个最大元素](https://leetcode-cn.com/problems/kth-largest-element-in-an-array/)|[go](./myheap/215.findKthLargest.go)|M|
@@ -67,4 +67,5 @@
6767
|1021|[删除最外层的括号](https://leetcode-cn.com/problems/remove-outermost-parentheses/)|[go](./mystack/1021.removeOuterParentheses.go)|S|
6868
|1108|[地址无效化](https://leetcode-cn.com/problems/defanging-an-ip-address/)|[go](./mystring/1108.defangIPaddr.go)|S|
6969
|1213|[三个有序数组的交集](https://leetcode-cn.com/problems/intersection-of-three-sorted-arrays/)|[go](./myarray/1213.arraysIntersection.go)|S|
70+
|1367|[二叉树中的列表](https://leetcode-cn.com/problems/linked-list-in-binary-tree/)|[go](./tree/1367.isSubPath.go)|M|
7071
||||[:top:](#Top)|

algorithms/kit/ListNode.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,17 @@ func NewListNode(val int) *ListNode {
1212

1313
// Ints2List convert []int to List
1414
func Ints2List(nums []int) *ListNode {
15+
if len(nums) == 0 {
16+
return nil
17+
}
18+
1519
dummy := &ListNode{Val: -1}
1620
curr := dummy
1721
for _, num := range nums {
1822
curr.Next = &ListNode{Val: num}
1923
curr = curr.Next
2024
}
25+
2126
return dummy.Next
2227
}
2328

algorithms/mystack/173.BSTIterator_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ package mystack
33
import (
44
"testing"
55

6-
"Leetcode/algorithms/kit"
7-
86
"github.com/stretchr/testify/assert"
7+
8+
"Leetcode/algorithms/kit"
99
)
1010

1111
// run: go test -v base.go 173.*

algorithms/tree/1367.isSubPath.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package tree
2+
3+
// 判断一个树是否存在链表的路径
4+
func isSubPath(head *ListNode, root *TreeNode) bool {
5+
if root == nil {
6+
return false
7+
}
8+
9+
// 先判断当前的节点,如果不对,再看左右子树
10+
return isMatch(head, root) || isSubPath(head, root.Left) || isSubPath(head, root.Right)
11+
}
12+
13+
// 判断二叉树中的每个节点为起点往下的路径是否与链表相匹配
14+
// head表示当前已经匹配到的链表节点,root表示二叉树的节点
15+
func isMatch(head *ListNode, root *TreeNode) bool {
16+
// 特判1:链表走完了,返回true
17+
if head == nil {
18+
return true
19+
}
20+
21+
// 特判2:链表没走完,树走完了,这肯定不行啊,返回false
22+
if root == nil {
23+
return false
24+
}
25+
26+
// 特判3:值不同,必定不是啊
27+
if head.Val != root.Val {
28+
return false
29+
}
30+
31+
// 正常逻辑:如果值相同,继续走,左子树和右子树有一个满足即可
32+
return isMatch(head.Next, root.Left) || isMatch(head.Next, root.Right)
33+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package tree
2+
3+
import (
4+
"Leetcode/algorithms/kit"
5+
"testing"
6+
)
7+
8+
// run: go test -v -run Test_isSubPath
9+
func Test_isSubPath(t *testing.T) {
10+
cases := []kit.CaseEntry{
11+
{
12+
Name: "x1",
13+
Input: map[string]interface{}{
14+
"head": []int{},
15+
"root": []int{1, 4, 4, null, 2, 2, null, 1, null, 6, 8, null, null, null, null, 1, 3},
16+
},
17+
Expected: true,
18+
},
19+
{
20+
Name: "x2",
21+
Input: map[string]interface{}{
22+
"head": []int{4, 2, 8},
23+
"root": []int{1, 4, 4, null, 2, 2, null, 1, null, 6, 8, null, null, null, null, 1, 3},
24+
},
25+
Expected: true,
26+
},
27+
{
28+
Name: "x3",
29+
Input: map[string]interface{}{
30+
"head": []int{1, 4, 2, 6},
31+
"root": []int{1, 4, 4, null, 2, 2, null, 1, null, 6, 8, null, null, null, null, 1, 3},
32+
},
33+
Expected: true,
34+
},
35+
{
36+
Name: "x4",
37+
Input: map[string]interface{}{
38+
"head": []int{1, 4, 2, 6, 8},
39+
"root": []int{1, 4, 4, null, 2, 2, null, 1, null, 6, 8, null, null, null, null, 1, 3},
40+
},
41+
Expected: false,
42+
},
43+
}
44+
45+
for _, c := range cases {
46+
t.Run(c.Name, func(t *testing.T) {
47+
input := c.Input.(map[string]interface{})
48+
head := kit.Ints2List(input["head"].([]int))
49+
root := kit.Ints2Tree(input["root"].([]int))
50+
if output := isSubPath(head, root); output != c.Expected.(bool) {
51+
t.Errorf("isSubPath()=%t, expected=%t", output, c.Expected)
52+
}
53+
})
54+
}
55+
}

algorithms/tree/173.BSTIterator.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package tree
2+
3+
// 二叉搜索树(Binary Search Tree)又叫 `二叉排序树` 他是一颗空树 或者具有以下特点:
4+
// 1. 针对某个节点,根节点上的值大于等于左子树的值,小于等于右子树的值
5+
// 2. 他的左右子树也分别是二叉排序树
6+
// 通过 `中序遍历` 可以得到一个升序的数组,这点很重要
7+
8+
import (
9+
"Leetcode/algorithms/kit"
10+
)
11+
12+
type BSTIterator struct {
13+
index int
14+
nums []int
15+
}
16+
17+
func NewBSTIterator(root *TreeNode) *BSTIterator {
18+
return &BSTIterator{
19+
index: -1,
20+
nums: kit.Tree2Inorder(root),
21+
}
22+
}
23+
24+
func (it *BSTIterator) Next() int {
25+
it.index += 1
26+
return it.nums[it.index]
27+
}
28+
29+
func (it *BSTIterator) HasNext() bool {
30+
return it.index+1 < len(it.nums)
31+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package tree
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
8+
"Leetcode/algorithms/kit"
9+
)
10+
11+
// run: go test -v -run Test_BSTIterator
12+
func Test_BSTIterator(t *testing.T) {
13+
assert := assert.New(t)
14+
ints := []int{7, 3, 15, null, null, 9, 20}
15+
root := kit.Ints2Tree(ints)
16+
17+
nums := kit.Tree2Inorder(root)
18+
19+
var i int
20+
it := NewBSTIterator(root)
21+
for it.HasNext() {
22+
assert.Equal(nums[i], it.Next(), "%d", nums[i])
23+
i++
24+
}
25+
}

algorithms/tree/base.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,5 @@ type TreeNode = kit.TreeNode
1414
// Stack ...
1515
type Stack = kit.Stack
1616

17-
// var TreeNode = kit.TreeNode
17+
// ListNode
18+
type ListNode = kit.ListNode

0 commit comments

Comments
 (0)