-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1367-LinkedListInBinaryTree.go
More file actions
208 lines (191 loc) · 6.88 KB
/
1367-LinkedListInBinaryTree.go
File metadata and controls
208 lines (191 loc) · 6.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
package main
// 1367. Linked List in Binary Tree
// Given a binary tree root and a linked list with head as the first node.
// Return True if all the elements in the linked list starting from the head correspond to some downward path connected in the binary tree otherwise return False.
// In this context downward path means a path that starts at some node and goes downwards.
// Example 1:
// 1
// / \
// 4 (4)
// \ /
// 2 (2)
// / / \
// 1 6 (8)
// / \
// 1 3
// <img src="https://assets.leetcode.com/uploads/2020/02/12/sample_1_1720.png" />
// Input: head = [4,2,8], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3]
// Output: true
// Explanation: Nodes in blue form a subpath in the binary Tree.
// Example 2:
// (1)
// / \
// 4 (4)
// \ /
// 2 (2)
// / / \
// 1 (6) 8
// / \
// 1 3
// <img src="https://assets.leetcode.com/uploads/2020/02/12/sample_2_1720.png" />
// Input: head = [1,4,2,6], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3]
// Output: true
// Example 3:
// Input: head = [1,4,2,6,8], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3]
// Output: false
// Explanation: There is no path in the binary tree that contains all the elements of the linked list from head.
// Constraints:
// The number of nodes in the tree will be in the range [1, 2500].
// The number of nodes in the list will be in the range [1, 100].
// 1 <= Node.val <= 100 for each node in the linked list and binary tree.
import "fmt"
// Definition for a binary tree node.
type TreeNode struct {
Val int
Left *TreeNode
Right *TreeNode
}
type ListNode struct {
Val int
Next *ListNode
}
// 打印链表
func printListNode(l *ListNode) {
if nil == l {
return
}
for {
if nil == l.Next {
fmt.Print(l.Val)
break
} else {
fmt.Print(l.Val, " -> ")
}
l = l.Next
}
fmt.Println()
}
// 数组创建链表
func makeListNode(arr []int) *ListNode {
if (len(arr) == 0) {
return nil
}
var l = (len(arr) - 1)
var head = &ListNode{arr[l], nil}
for i := l - 1; i >= 0; i-- {
var n = &ListNode{arr[i], head}
head = n
}
return head
}
/**
* Definition for singly-linked list.
* type ListNode struct {
* Val int
* Next *ListNode
* }
*/
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
func isSubPath1(head *ListNode, root *TreeNode) bool {
var dfs func(head *ListNode, cur *ListNode, root *TreeNode) bool
dfs = func(head *ListNode, cur *ListNode, root *TreeNode) bool {
if cur == nil { return true } // reach the end of the linked list, return true (successful match)
if root == nil { return false } // reach the end of a path in the tree without fully matching the list, return false
// Match the current tree node with the current linked list node
// If no match, but the tree node matches the head of the linked list, start a new match
// Otherwise, reset `cur` to `head` to attempt matching the linked list from scratch
if cur.Val == root.Val {
cur = cur.Next
} else if head.Val == root.Val {
head = head.Next
} else {
cur = head
}
// Continue dfs down both left and right children
return dfs(head, cur, root.Left) || dfs(head, cur, root.Right)
}
return dfs(head, head, root)
}
func isSubPath(head *ListNode, root *TreeNode) bool {
// 以当前根节点开始是否有指定链表的路径
var isPath func(head *ListNode, root *TreeNode) bool
isPath = func(head *ListNode, root *TreeNode) bool {
if head == nil { return true }
if root == nil { return false }
if root.Val != head.Val { return false }
if isPath(head.Next, root.Left) { return true } // left
return isPath(head.Next, root.Right) // right
}
if head == nil { return true }
if root == nil { return false }
if isPath(head, root) { return true } // 深度优先遍历判断 以 每个节点为根是否符合
if isSubPath(head, root.Left) { return true } // 左边能走就不用走右边了
return isSubPath(head, root.Right)
}
func main() {
// Example 1:
// 1
// / \
// 4 (4)
// \ /
// 2 (2)
// / / \
// 1 6 (8)
// / \
// 1 3
// <img src="https://assets.leetcode.com/uploads/2020/02/12/sample_1_1720.png" />
// Input: head = [4,2,8], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3]
// Output: true
// Explanation: Nodes in blue form a subpath in the binary Tree.
tree1 := &TreeNode{
1,
&TreeNode{4, nil, &TreeNode{2, &TreeNode{1, nil, nil,}, nil,}, },
&TreeNode{4, &TreeNode{2, &TreeNode{6, nil, nil,}, &TreeNode{8, &TreeNode{1, nil, nil,}, &TreeNode{3, nil, nil,},},}, nil, },
}
list1 := makeListNode([]int{4,2,8})
printListNode(list1) // 4 -> 2 -> 8
fmt.Println(isSubPath(list1,tree1)) // true
// Example 2:
// (1)
// / \
// 4 (4)
// \ /
// 2 (2)
// / / \
// 1 (6) 8
// / \
// 1 3
// <img src="https://assets.leetcode.com/uploads/2020/02/12/sample_2_1720.png" />
// Input: head = [1,4,2,6], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3]
// Output: true
tree2 := &TreeNode{
1,
&TreeNode{4, nil, &TreeNode{2, &TreeNode{1, nil, nil,}, nil,}, },
&TreeNode{4, &TreeNode{2, &TreeNode{6, nil, nil,}, &TreeNode{8, &TreeNode{1, nil, nil,}, &TreeNode{3, nil, nil,},},}, nil, },
}
list2 := makeListNode([]int{1,4,2,6})
printListNode(list2) // 1 -> 4 -> 2 -> 6
fmt.Println(isSubPath(list2,tree2)) // true
// Example 3:
// Input: head = [1,4,2,6,8], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3]
// Output: false
// Explanation: There is no path in the binary tree that contains all the elements of the linked list from head.
tree3 := &TreeNode{
1,
&TreeNode{4, nil, &TreeNode{2, &TreeNode{1, nil, nil,}, nil,}, },
&TreeNode{4, &TreeNode{2, &TreeNode{6, nil, nil,}, &TreeNode{8, &TreeNode{1, nil, nil,}, &TreeNode{3, nil, nil,},},}, nil, },
}
list3 := makeListNode([]int{1,4,2,6,8})
printListNode(list3) // 1 -> 4 -> 2 -> 6 -> 8
fmt.Println(isSubPath(list3,tree3)) // false
fmt.Println(isSubPath1(list1,tree1)) // true
fmt.Println(isSubPath1(list2,tree2)) // true
fmt.Println(isSubPath1(list3,tree3)) // false
}