Check If a String Is a Valid Sequence from Root to Leaves Path in a Binary Tree — LeetCode 1430 Python Solution

MediumLeetCode PremiumTreeDepth-First SearchBreadth-First SearchBinary Tree
Problem
#1430
Reading time
3 min

The problem

Given a binary tree where each path going from the root to any leaf form a valid sequence, check if a given string is a valid sequence in such binary tree. We get the given string from the concatenation of an array of integers arr and the concatenation of all values of the nodes along a path results in a sequence in the given binary tree.

Example

Input
root = [0,1,0,0,1,0,null,null,1,0,0], arr = [0,1,0,1]
Output
true
Explanation
The path 0 -> 1 -> 0 -> 1 is a valid sequence (green color in the figure).

Python solution

Python
# 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 isValidSequence(self, root: TreeNode, arr: List[int]) -> bool:
        def dfs(root, u):
            if root is None or root.val != arr[u]:
                return False
            if u == len(arr) - 1:
                return root.left is None and root.right is None
            return dfs(root.left, u + 1) or dfs(root.right, u + 1)

        return dfs(root, 0)

Complexity

MeasureComplexity
TimeO(V+E)
SpaceO(V) auxiliary

Pattern: Tree Traversal

Choose the order — preorder, inorder, postorder, level — and the problem solves itself. LeetCode 1430. Check If a String Is a Valid Sequence from Root to Leaves Path in a Binary Tree is filed here because LeetCode tags it Tree and Binary Tree, which is the vocabulary this hub collects.

The tree traversal guide has the Python template for the pattern and the 225 LeetCode problems that use it.

Related problems

Frequently asked questions

How hard is LeetCode 1430. Check If a String Is a Valid Sequence from Root to Leaves Path in a Binary Tree?
LeetCode 1430. Check If a String Is a Valid Sequence from Root to Leaves Path in a Binary Tree is rated Medium on LeetCode.
What is the time complexity of LeetCode 1430. Check If a String Is a Valid Sequence from Root to Leaves Path in a Binary Tree?
The Python solution on this page runs in O(V+E).
What is the space complexity of LeetCode 1430. Check If a String Is a Valid Sequence from Root to Leaves Path in a Binary Tree?
The Python solution on this page uses O(V) auxiliary space.
What topics does LeetCode 1430. Check If a String Is a Valid Sequence from Root to Leaves Path in a Binary Tree cover?
LeetCode 1430. Check If a String Is a Valid Sequence from Root to Leaves Path in a Binary Tree is tagged Tree, Depth-First Search, Breadth-First Search and Binary Tree on LeetCode.
Is LeetCode 1430. Check If a String Is a Valid Sequence from Root to Leaves Path in a Binary Tree a premium problem?
Yes. LeetCode 1430. Check If a String Is a Valid Sequence from Root to Leaves Path in a Binary Tree is a LeetCode Premium problem, so the full statement and test cases require a paid LeetCode subscription.

Stuck on problems like this in a live interview?

Stealth Interview is a desktop app for macOS and Windows. It reads the problem off your screen and returns a working solution with a step-by-step explanation and its time and space complexity — invisible to screen sharing.

Get Stealth Interview