forked from YuriSpiridonov/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
1367.LinkedListinBinaryTree.py
86 lines (71 loc) · 2.91 KB
/
1367.LinkedListinBinaryTree.py
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
'''
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:
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:
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:
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.
'''
#Difficulty: Medium
#67 / 67 test cases passed.
#Runtime: 200 ms
#Memory Usage: 16.1 MB
#Runtime: 200 ms, faster than 11.80% of Python3 online submissions for Linked List in Binary Tree.
#Memory Usage: 16.1 MB, less than 63.92% of Python3 online submissions for Linked List in Binary Tree.
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
#
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def isSubPath(self, head: ListNode, root: TreeNode) -> bool:
self.result = 0
self.dfs(root, head)
return self.result
def dfs(self, root, head):
if not root:
return False
if root.val == head.val:
self.result = max(self.result, self.headFollower(root, head))
self.dfs(root.left, head)
self.dfs(root.right, head)
def headFollower(self, root, head):
if root.left and head.next and root.left.val == head.next.val and root.right and head.next and root.right.val == head.next.val:
return max(self.headFollower(root.left, head.next), self.headFollower(root.right, head.next))
if root.left and head.next and root.left.val == head.next.val:
return self.headFollower(root.left, head.next)
if root.right and head.next and root.right.val == head.next.val:
return self.headFollower(root.right, head.next)
return root.val == head.val and head.next == None