Skip to content

Commit cfc58ab

Browse files
committed
feat: add solutions to lc problem: No.1367
No.1367.Linked List in Binary Tree
1 parent 52236dd commit cfc58ab

File tree

6 files changed

+253
-22
lines changed

6 files changed

+253
-22
lines changed

solution/1300-1399/1367.Linked List in Binary Tree/README.md

Lines changed: 96 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,19 @@
5252

5353
<!-- 这里可写通用的实现逻辑 -->
5454

55+
**方法一:递归**
56+
57+
我们设计一个递归函数 $dfs(head, root)$,表示链表 $head$ 是否是以 $root$ 为起点的路径上的节点值一一对应的子路径。函数 $dfs(head, root)$ 的逻辑如下:
58+
59+
- 如果链表 $head$ 为空,说明链表已经遍历完了,返回 `true`
60+
- 如果二叉树 $root$ 为空,说明二叉树已经遍历完了,但链表还没遍历完,返回 `false`
61+
- 如果二叉树 $root$ 的值与链表 $head$ 的值不相等,返回 `false`
62+
- 否则,返回 $dfs(head.next, root.left)$ 或 $dfs(head.next, root.right)$。
63+
64+
我们在主函数中,对二叉树的每个节点调用 $dfs(head, root)$,只要有一个返回 `true`,就说明链表是二叉树的子路径,返回 `true`;如果所有节点都返回 `false`,说明链表不是二叉树的子路径,返回 `false`
65+
66+
时间复杂度 $O(n^2),空间复杂度 O(n)$。其中 $n$ 是二叉树的节点数。
67+
5568
<!-- tabs:start -->
5669

5770
### **Python3**
@@ -71,13 +84,11 @@
7184
# self.left = left
7285
# self.right = right
7386
class Solution:
74-
def isSubPath(self, head: ListNode, root: TreeNode) -> bool:
87+
def isSubPath(self, head: Optional[ListNode], root: Optional[TreeNode]) -> bool:
7588
def dfs(head, root):
7689
if head is None:
7790
return True
78-
if root is None:
79-
return False
80-
if root.val != head.val:
91+
if root is None or root.val != head.val:
8192
return False
8293
return dfs(head.next, root.left) or dfs(head.next, root.right)
8394

@@ -132,14 +143,92 @@ class Solution {
132143
if (head == null) {
133144
return true;
134145
}
135-
if (root == null) {
146+
if (root == null || head.val != root.val) {
136147
return false;
137148
}
138-
if (root.val != head.val) {
149+
return dfs(head.next, root.left) || dfs(head.next, root.right);
150+
}
151+
}
152+
```
153+
154+
### **C++**
155+
156+
```cpp
157+
/**
158+
* Definition for singly-linked list.
159+
* struct ListNode {
160+
* int val;
161+
* ListNode *next;
162+
* ListNode() : val(0), next(nullptr) {}
163+
* ListNode(int x) : val(x), next(nullptr) {}
164+
* ListNode(int x, ListNode *next) : val(x), next(next) {}
165+
* };
166+
*/
167+
/**
168+
* Definition for a binary tree node.
169+
* struct TreeNode {
170+
* int val;
171+
* TreeNode *left;
172+
* TreeNode *right;
173+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
174+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
175+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
176+
* };
177+
*/
178+
class Solution {
179+
public:
180+
bool isSubPath(ListNode* head, TreeNode* root) {
181+
if (!root) {
139182
return false;
140183
}
141-
return dfs(head.next, root.left) || dfs(head.next, root.right);
184+
return dfs(head, root) || isSubPath(head, root->left) || isSubPath(head, root->right);
142185
}
186+
187+
bool dfs(ListNode* head, TreeNode* root) {
188+
if (!head) {
189+
return true;
190+
}
191+
if (!root || head->val != root->val) {
192+
return false;
193+
}
194+
return dfs(head->next, root->left) || dfs(head->next, root->right);
195+
}
196+
};
197+
```
198+
199+
### **Go**
200+
201+
```go
202+
/**
203+
* Definition for singly-linked list.
204+
* type ListNode struct {
205+
* Val int
206+
* Next *ListNode
207+
* }
208+
*/
209+
/**
210+
* Definition for a binary tree node.
211+
* type TreeNode struct {
212+
* Val int
213+
* Left *TreeNode
214+
* Right *TreeNode
215+
* }
216+
*/
217+
func isSubPath(head *ListNode, root *TreeNode) bool {
218+
if root == nil {
219+
return false
220+
}
221+
return dfs(head, root) || isSubPath(head, root.Left) || isSubPath(head, root.Right)
222+
}
223+
224+
func dfs(head *ListNode, root *TreeNode) bool {
225+
if head == nil {
226+
return true
227+
}
228+
if root == nil || head.Val != root.Val {
229+
return false
230+
}
231+
return dfs(head.Next, root.Left) || dfs(head.Next, root.Right)
143232
}
144233
```
145234

solution/1300-1399/1367.Linked List in Binary Tree/README_EN.md

Lines changed: 83 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,11 @@
6666
# self.left = left
6767
# self.right = right
6868
class Solution:
69-
def isSubPath(self, head: ListNode, root: TreeNode) -> bool:
69+
def isSubPath(self, head: Optional[ListNode], root: Optional[TreeNode]) -> bool:
7070
def dfs(head, root):
7171
if head is None:
7272
return True
73-
if root is None:
74-
return False
75-
if root.val != head.val:
73+
if root is None or root.val != head.val:
7674
return False
7775
return dfs(head.next, root.left) or dfs(head.next, root.right)
7876

@@ -125,14 +123,92 @@ class Solution {
125123
if (head == null) {
126124
return true;
127125
}
128-
if (root == null) {
126+
if (root == null || head.val != root.val) {
129127
return false;
130128
}
131-
if (root.val != head.val) {
129+
return dfs(head.next, root.left) || dfs(head.next, root.right);
130+
}
131+
}
132+
```
133+
134+
### **C++**
135+
136+
```cpp
137+
/**
138+
* Definition for singly-linked list.
139+
* struct ListNode {
140+
* int val;
141+
* ListNode *next;
142+
* ListNode() : val(0), next(nullptr) {}
143+
* ListNode(int x) : val(x), next(nullptr) {}
144+
* ListNode(int x, ListNode *next) : val(x), next(next) {}
145+
* };
146+
*/
147+
/**
148+
* Definition for a binary tree node.
149+
* struct TreeNode {
150+
* int val;
151+
* TreeNode *left;
152+
* TreeNode *right;
153+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
154+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
155+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
156+
* };
157+
*/
158+
class Solution {
159+
public:
160+
bool isSubPath(ListNode* head, TreeNode* root) {
161+
if (!root) {
132162
return false;
133163
}
134-
return dfs(head.next, root.left) || dfs(head.next, root.right);
164+
return dfs(head, root) || isSubPath(head, root->left) || isSubPath(head, root->right);
165+
}
166+
167+
bool dfs(ListNode* head, TreeNode* root) {
168+
if (!head) {
169+
return true;
170+
}
171+
if (!root || head->val != root->val) {
172+
return false;
173+
}
174+
return dfs(head->next, root->left) || dfs(head->next, root->right);
135175
}
176+
};
177+
```
178+
179+
### **Go**
180+
181+
```go
182+
/**
183+
* Definition for singly-linked list.
184+
* type ListNode struct {
185+
* Val int
186+
* Next *ListNode
187+
* }
188+
*/
189+
/**
190+
* Definition for a binary tree node.
191+
* type TreeNode struct {
192+
* Val int
193+
* Left *TreeNode
194+
* Right *TreeNode
195+
* }
196+
*/
197+
func isSubPath(head *ListNode, root *TreeNode) bool {
198+
if root == nil {
199+
return false
200+
}
201+
return dfs(head, root) || isSubPath(head, root.Left) || isSubPath(head, root.Right)
202+
}
203+
204+
func dfs(head *ListNode, root *TreeNode) bool {
205+
if head == nil {
206+
return true
207+
}
208+
if root == nil || head.Val != root.Val {
209+
return false
210+
}
211+
return dfs(head.Next, root.Left) || dfs(head.Next, root.Right)
136212
}
137213
```
138214

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* struct ListNode {
4+
* int val;
5+
* ListNode *next;
6+
* ListNode() : val(0), next(nullptr) {}
7+
* ListNode(int x) : val(x), next(nullptr) {}
8+
* ListNode(int x, ListNode *next) : val(x), next(next) {}
9+
* };
10+
*/
11+
/**
12+
* Definition for a binary tree node.
13+
* struct TreeNode {
14+
* int val;
15+
* TreeNode *left;
16+
* TreeNode *right;
17+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
18+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
19+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
20+
* };
21+
*/
22+
class Solution {
23+
public:
24+
bool isSubPath(ListNode* head, TreeNode* root) {
25+
if (!root) {
26+
return false;
27+
}
28+
return dfs(head, root) || isSubPath(head, root->left) || isSubPath(head, root->right);
29+
}
30+
31+
bool dfs(ListNode* head, TreeNode* root) {
32+
if (!head) {
33+
return true;
34+
}
35+
if (!root || head->val != root->val) {
36+
return false;
37+
}
38+
return dfs(head->next, root->left) || dfs(head->next, root->right);
39+
}
40+
};
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* type ListNode struct {
4+
* Val int
5+
* Next *ListNode
6+
* }
7+
*/
8+
/**
9+
* Definition for a binary tree node.
10+
* type TreeNode struct {
11+
* Val int
12+
* Left *TreeNode
13+
* Right *TreeNode
14+
* }
15+
*/
16+
func isSubPath(head *ListNode, root *TreeNode) bool {
17+
if root == nil {
18+
return false
19+
}
20+
return dfs(head, root) || isSubPath(head, root.Left) || isSubPath(head, root.Right)
21+
}
22+
23+
func dfs(head *ListNode, root *TreeNode) bool {
24+
if head == nil {
25+
return true
26+
}
27+
if root == nil || head.Val != root.Val {
28+
return false
29+
}
30+
return dfs(head.Next, root.Left) || dfs(head.Next, root.Right)
31+
}

solution/1300-1399/1367.Linked List in Binary Tree/Solution.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,7 @@ private boolean dfs(ListNode head, TreeNode root) {
3535
if (head == null) {
3636
return true;
3737
}
38-
if (root == null) {
39-
return false;
40-
}
41-
if (root.val != head.val) {
38+
if (root == null || head.val != root.val) {
4239
return false;
4340
}
4441
return dfs(head.next, root.left) || dfs(head.next, root.right);

solution/1300-1399/1367.Linked List in Binary Tree/Solution.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,11 @@
1010
# self.left = left
1111
# self.right = right
1212
class Solution:
13-
def isSubPath(self, head: ListNode, root: TreeNode) -> bool:
13+
def isSubPath(self, head: Optional[ListNode], root: Optional[TreeNode]) -> bool:
1414
def dfs(head, root):
1515
if head is None:
1616
return True
17-
if root is None:
18-
return False
19-
if root.val != head.val:
17+
if root is None or root.val != head.val:
2018
return False
2119
return dfs(head.next, root.left) or dfs(head.next, root.right)
2220

0 commit comments

Comments
 (0)