List LeetCode 1367. Linked List in Binary Tree binary tree [Medium] [Python] [DFS]
Problem
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:
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:
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:
1 <= node.val <= 100
for each node in the linked list and binary tree.- The given linked list will contain between
1
and100
nodes. - The given binary tree will contain between
1
and2500
nodes.
problem
To give you a root
root of a binary tree and head
for the first node of the list.
If the binary tree, there has been a downward path, and the value of each point exactly correspond to head
headed the list the value of each node, and then you return True
it, otherwise return False
.
Has been downward path means: start from a node in the tree, it has been a continuous downward path.
Example 1:
输入:head = [4,2,8], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3]
输出:true
解释:树中蓝色的节点构成了与链表对应的子路径。
Example 2:
输入:head = [1,4,2,6], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3]
输出:true
Example 3:
输入:head = [1,4,2,6,8], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3]
输出:false
解释:二叉树中不存在一一对应链表的路径。
prompt:
- And a binary value for each node in the linked list are satisfied
1 <= node.val <= 100
. - The number of nodes included in the list
1
to100
between. - The number of nodes in the binary tree
1
to2500
between.
Thinking
DFS double
第一重:找到起点。先判断当前节点,如果不对就判断左子树和右子树。
第二重:从找到的起点开始判断剩下的点。
Python3 Code
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
def isSubPath(self, head: ListNode, root: TreeNode) -> bool:
if head == None:
return True
if root == None:
return False
# judge root, then judge root.left and root.right
return self.isSub(head, root) or self.isSubPath(head, root.left) or self.isSubPath(head, root.right)
def isSub(self, head, node):
# list is over
if head == None:
return True
# list is not over and tree is over
if node == None:
return False
# not equal
if not head.val == node.val:
return False
# equal, then left and right
return self.isSub(head.next, node.left) or self.isSub(head.next, node.right)