LeetCode 1367. Linked List in Binary Tree Solution in Java, C++, Python & More | Explanation + Code

CoderIndeed
0
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:

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:

  • 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.

Solutions

Solution 1: Recursion

We design a recursive function dfs(head, root), which indicates whether the linked list head corresponds to a subpath on the path starting with root in the binary tree. The logic of the function dfs(head, root) is as follows:

  • If the linked list head is empty, it means that the linked list has been traversed, return true;
  • If the binary tree root is empty, it means that the binary tree has been traversed, but the linked list has not been traversed yet, return false;
  • If the value of the binary tree root is not equal to the value of the linked list head, return false;
  • Otherwise, return dfs(head.next, root.left) or dfs(head.next, root.right).

In the main function, we call dfs(head, root) for each node of the binary tree. As long as one returns true, it means that the linked list is a subpath of the binary tree, return true; if all nodes return false, it means that the linked list is not a subpath of the binary tree, return false.

The time complexity is O(n2), and the space complexity is O(n). Where n is the number of nodes in the binary tree.

PythonJavaC++GoTypeScriptRust
# 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: Optional[ListNode], root: Optional[TreeNode]) -> bool: def dfs(head, root): if head is None: return True if root is None or root.val != head.val: return False return dfs(head.next, root.left) or dfs(head.next, root.right) if root is None: return False return ( dfs(head, root) or self.isSubPath(head, root.left) or self.isSubPath(head, root.right) )(code-box)

Post a Comment

0Comments

Post a Comment (0)

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Check Now
Accept !