Skip to main content

Command Palette

Search for a command to run...

Solving Leetcode Interviews in Seconds with AI: Linked List in Binary Tree

Updated
3 min read

Introduction

In this blog post, we will explore how to solve the LeetCode problem "1367" using AI. LeetCode is a popular platform for preparing for coding interviews, and with the help of AI tools like Chatmagic, we can generate solutions quickly and efficiently - helping you pass the interviews and get the job offer without having to study for months.

Problem Statement

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.

Explanation

Here's a breakdown of the solution and the Python code:

  • High-Level Approach:

    • Start by checking if the linked list is empty. If it is, return True.
    • Recursively traverse the binary tree. At each node, check if the linked list path matches the tree path starting from that node.
    • The check_path function recursively checks if the linked list matches a path downwards in the tree starting from a given node.
  • Complexity Analysis:

    • Runtime Complexity: O(N * M), where N is the number of nodes in the binary tree and M is the number of nodes in the linked list. In the worst case, we might have to traverse the linked list for each node in the tree.
    • Storage Complexity: O(H), where H is the height of the binary tree, due to the recursive call stack. In the worst case (skewed tree), H can be N.

Code

    class ListNode:
    def __init__(self, val=0, next=None):
        self.val = val
        self.next = next

class TreeNode:
    def __init__(self, val=0, left=None, right=None):
        self.val = val
        self.left = left
        self.right = right

def isSubPath(head: ListNode, root: TreeNode) -> bool:
    """
    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.
    """

    if not head:
        return True
    if not root:
        return False

    def check_path(node: TreeNode, head: ListNode) -> bool:
        if not head:
            return True
        if not node:
            return False
        if node.val != head.val:
            return False

        return check_path(node.left, head.next) or check_path(node.right, head.next)

    def traverse_tree(node: TreeNode, head: ListNode) -> bool:
        if not node:
            return False

        return check_path(node, head) or traverse_tree(node.left, head) or traverse_tree(node.right, head)

    return traverse_tree(root, head)

More from this blog

C

Chatmagic blog

2894 posts