[leetcode] 1367. Linked List in Binary Tree

Description

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:
Insert picture description here

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:
Insert picture description here

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. 1 <= node.val <= 100 for each node in the linked list and binary tree.
  2. The given linked list will contain between 1 and 100 nodes.
  3. The given binary tree will contain between 1 and 2500 nodes.

analysis

The meaning of the question is: given a binary tree and a linked list, ask whether the linked list exists in a certain part of the path of the binary tree. One of the ways of thinking is also very simple. It is a violent solution to judge that the linked list can correspond to the partial path starting with the current node directly during the traversal of the binary tree.

  • Of course, I also found another solution to turn the linked list into a string, and then when the binary tree is recursively traversed to see if the string is a substring of the recursive string, the idea is relatively new.
  • Of course, there is a solution to dp, haha, if you are interested, you can study it.

Code

# 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 check(self,head,root):
        if(head is None):
            return True
        if(root is None):
            return False
        if(root and head.val!=root.val):
            return False
        l=self.check(head.next,root.left)
        r=self.check(head.next,root.right)
        return l or r
    
    def isSubPath(self, head: ListNode, root: TreeNode) -> bool:
        if(head is None):
            return True
        if(root is None):
            return False
        c=False
        if(root and root.val==head.val):
            c=self.check(head,root)
        l=self.isSubPath(head,root.left)
        r=self.isSubPath(head,root.right)
        return l or r or c
        

references

[LeetCode] [Python] Recursive Solution, O(N) Time

Guess you like

Origin blog.csdn.net/w5688414/article/details/109319792