Linked List in Binary Tree

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 head correspond to some downward path connected in the binary tree; otherwise, return false.

In this context, a downward path means a path that starts at some node and goes downwards.

Example 1
    [4] -> [2] -> [8] -> null

                1
            /       \
        4               4
          \           /
            2       2
           /       / \
          1       6   8
                     / \
                    1   3
Inputhead = [4,2,8], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3]
Outputtrue
Nodes in blue form a subpath in the binary tree.
Example 2
[1] -> [4] -> [2] -> [6] -> null

            1
           / \
          4   4
           \ /
            2
           / \
          1   2
             / \
            6   8
               / \
              1   3
Inputhead = [1,4,2,6], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3]
Outputtrue
The linked list values appear along a downward path in the binary tree.

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.

Asked at 5 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate