Skip to content

Commit c304aae

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

File tree

4 files changed

+213
-6
lines changed

4 files changed

+213
-6
lines changed

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

+71-3
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@
4848
<li>二叉树包含的节点数目在&nbsp;<code>1</code>&nbsp;到&nbsp;<code>2500</code>&nbsp;之间。</li>
4949
</ul>
5050

51-
5251
## 解法
5352

5453
<!-- 这里可写通用的实现逻辑 -->
@@ -60,15 +59,84 @@
6059
<!-- 这里可写当前语言的特殊实现逻辑 -->
6160

6261
```python
63-
62+
# Definition for singly-linked list.
63+
# class ListNode:
64+
# def __init__(self, val=0, next=None):
65+
# self.val = val
66+
# self.next = next
67+
# Definition for a binary tree node.
68+
# class TreeNode:
69+
# def __init__(self, val=0, left=None, right=None):
70+
# self.val = val
71+
# self.left = left
72+
# self.right = right
73+
class Solution:
74+
def isSubPath(self, head: ListNode, root: TreeNode) -> bool:
75+
def dfs(head, root):
76+
if head is None:
77+
return True
78+
if root is None:
79+
return False
80+
if root.val != head.val:
81+
return False
82+
return dfs(head.next, root.left) or dfs(head.next, root.right)
83+
84+
if root is None:
85+
return False
86+
return dfs(head, root) or self.isSubPath(head, root.left) or self.isSubPath(head, root.right)
6487
```
6588

6689
### **Java**
6790

6891
<!-- 这里可写当前语言的特殊实现逻辑 -->
6992

7093
```java
71-
94+
/**
95+
* Definition for singly-linked list.
96+
* public class ListNode {
97+
* int val;
98+
* ListNode next;
99+
* ListNode() {}
100+
* ListNode(int val) { this.val = val; }
101+
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
102+
* }
103+
*/
104+
/**
105+
* Definition for a binary tree node.
106+
* public class TreeNode {
107+
* int val;
108+
* TreeNode left;
109+
* TreeNode right;
110+
* TreeNode() {}
111+
* TreeNode(int val) { this.val = val; }
112+
* TreeNode(int val, TreeNode left, TreeNode right) {
113+
* this.val = val;
114+
* this.left = left;
115+
* this.right = right;
116+
* }
117+
* }
118+
*/
119+
class Solution {
120+
public boolean isSubPath(ListNode head, TreeNode root) {
121+
if (root == null) {
122+
return false;
123+
}
124+
return dfs(head, root) || isSubPath(head, root.left) || isSubPath(head, root.right);
125+
}
126+
127+
private boolean dfs(ListNode head, TreeNode root) {
128+
if (head == null) {
129+
return true;
130+
}
131+
if (root == null) {
132+
return false;
133+
}
134+
if (root.val != head.val) {
135+
return false;
136+
}
137+
return dfs(head.next, root.left) || dfs(head.next, root.right);
138+
}
139+
}
72140
```
73141

74142
### **...**

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

+71-3
Original file line numberDiff line numberDiff line change
@@ -47,21 +47,89 @@
4747
<li><code>1 &lt;= Node.val&nbsp;&lt;= 100</code>&nbsp;for each node in the linked list and binary tree.</li>
4848
</ul>
4949

50-
5150
## Solutions
5251

5352
<!-- tabs:start -->
5453

5554
### **Python3**
5655

5756
```python
58-
57+
# Definition for singly-linked list.
58+
# class ListNode:
59+
# def __init__(self, val=0, next=None):
60+
# self.val = val
61+
# self.next = next
62+
# Definition for a binary tree node.
63+
# class TreeNode:
64+
# def __init__(self, val=0, left=None, right=None):
65+
# self.val = val
66+
# self.left = left
67+
# self.right = right
68+
class Solution:
69+
def isSubPath(self, head: ListNode, root: TreeNode) -> bool:
70+
def dfs(head, root):
71+
if head is None:
72+
return True
73+
if root is None:
74+
return False
75+
if root.val != head.val:
76+
return False
77+
return dfs(head.next, root.left) or dfs(head.next, root.right)
78+
79+
if root is None:
80+
return False
81+
return dfs(head, root) or self.isSubPath(head, root.left) or self.isSubPath(head, root.right)
5982
```
6083

6184
### **Java**
6285

6386
```java
64-
87+
/**
88+
* Definition for singly-linked list.
89+
* public class ListNode {
90+
* int val;
91+
* ListNode next;
92+
* ListNode() {}
93+
* ListNode(int val) { this.val = val; }
94+
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
95+
* }
96+
*/
97+
/**
98+
* Definition for a binary tree node.
99+
* public class TreeNode {
100+
* int val;
101+
* TreeNode left;
102+
* TreeNode right;
103+
* TreeNode() {}
104+
* TreeNode(int val) { this.val = val; }
105+
* TreeNode(int val, TreeNode left, TreeNode right) {
106+
* this.val = val;
107+
* this.left = left;
108+
* this.right = right;
109+
* }
110+
* }
111+
*/
112+
class Solution {
113+
public boolean isSubPath(ListNode head, TreeNode root) {
114+
if (root == null) {
115+
return false;
116+
}
117+
return dfs(head, root) || isSubPath(head, root.left) || isSubPath(head, root.right);
118+
}
119+
120+
private boolean dfs(ListNode head, TreeNode root) {
121+
if (head == null) {
122+
return true;
123+
}
124+
if (root == null) {
125+
return false;
126+
}
127+
if (root.val != head.val) {
128+
return false;
129+
}
130+
return dfs(head.next, root.left) || dfs(head.next, root.right);
131+
}
132+
}
65133
```
66134

67135
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* public class ListNode {
4+
* int val;
5+
* ListNode next;
6+
* ListNode() {}
7+
* ListNode(int val) { this.val = val; }
8+
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
9+
* }
10+
*/
11+
/**
12+
* Definition for a binary tree node.
13+
* public class TreeNode {
14+
* int val;
15+
* TreeNode left;
16+
* TreeNode right;
17+
* TreeNode() {}
18+
* TreeNode(int val) { this.val = val; }
19+
* TreeNode(int val, TreeNode left, TreeNode right) {
20+
* this.val = val;
21+
* this.left = left;
22+
* this.right = right;
23+
* }
24+
* }
25+
*/
26+
class Solution {
27+
public boolean isSubPath(ListNode head, TreeNode root) {
28+
if (root == null) {
29+
return false;
30+
}
31+
return dfs(head, root) || isSubPath(head, root.left) || isSubPath(head, root.right);
32+
}
33+
34+
private boolean dfs(ListNode head, TreeNode root) {
35+
if (head == null) {
36+
return true;
37+
}
38+
if (root == null) {
39+
return false;
40+
}
41+
if (root.val != head.val) {
42+
return false;
43+
}
44+
return dfs(head.next, root.left) || dfs(head.next, root.right);
45+
}
46+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Definition for singly-linked list.
2+
# class ListNode:
3+
# def __init__(self, val=0, next=None):
4+
# self.val = val
5+
# self.next = next
6+
# Definition for a binary tree node.
7+
# class TreeNode:
8+
# def __init__(self, val=0, left=None, right=None):
9+
# self.val = val
10+
# self.left = left
11+
# self.right = right
12+
class Solution:
13+
def isSubPath(self, head: ListNode, root: TreeNode) -> bool:
14+
def dfs(head, root):
15+
if head is None:
16+
return True
17+
if root is None:
18+
return False
19+
if root.val != head.val:
20+
return False
21+
return dfs(head.next, root.left) or dfs(head.next, root.right)
22+
23+
if root is None:
24+
return False
25+
return dfs(head, root) or self.isSubPath(head, root.left) or self.isSubPath(head, root.right)

0 commit comments

Comments
 (0)