-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck-If-a-string-is-a-valid-sequence-from-root-to-leaves-path-in-a-binary-tree.py
executable file
·104 lines (84 loc) · 2.74 KB
/
check-If-a-string-is-a-valid-sequence-from-root-to-leaves-path-in-a-binary-tree.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# -*- coding: utf-8 -*-
"""
Created on Thu Apr 30 13:07:28 2020
@author: johnoyegbite
"""
# SOLVED!
"""
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 1:
0
/ \
1 0
/ \ /
0 1 0
\ / \
1 0 0
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).
Other valid sequences are:
0 -> 1 -> 1 -> 0
0 -> 0 -> 0
Example 2:
0
/ \
1 0
/ \ /
0 1 0
\ / \
1 0 0
Input: root = [0,1,0,0,1,0,null,null,1,0,0], arr = [0,0,1]
Output: false
Explanation: The path 0 -> 0 -> 1 does not exist,
therefore it is not even a sequence.
Example 3:
0
/ \
1 0
/ \ /
0 1 0
\ / \
1 0 0
Input: root = [0,1,0,0,1,0,null,null,1,0,0], arr = [0,1,1]
Output: false
Explanation: The path 0 -> 1 -> 1 is a sequence,
but it is not a valid sequence.
Constraints:
1 <= arr.length <= 5000
0 <= arr[i] <= 9
Each node's value is between [0 - 9].
"""
# 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 traverseTree(self, node: TreeNode, i: int, arr: list[int]) -> bool:
# if arr is longer or shorter than path
if (not node and i < len(arr)) or (node and i >= len(arr)):
return False
# if node is empty or if we meet a node and its value different
# from the value at the arr current index
if not node or node.val != arr[i]:
return False
# if we have completed the path and it's a leaf node
if not node.left and not node.right and i == len(arr)-1:
return True
is_left_valid = self.traverseTree(node.left, i+1, arr)
is_left_valid = self.traverseTree(node.right, i+1, arr)
return is_left_valid or is_left_valid
def isValidSequence(self, root: TreeNode, arr: list[int]) -> bool:
# starting from the first element in the array
first_idx = 0
return self.traverseTree(root, first_idx, arr)