题目
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:
1 | Input: root = [0,1,0,0,1,0,null,null,1,0,0], arr = [0,1,0,1] |
Example 2:
1 | Input: root = [0,1,0,0,1,0,null,null,1,0,0], arr = [0,0,1] |
Example 3:
1 | Input: root = [0,1,0,0,1,0,null,null,1,0,0], arr = [0,1,1] |
Constraints:
1 <= arr.length <= 5000
0 <= arr[i] <= 9
- Each node’s value is between [0 - 9].
思路
DFS.
注意是否达到了叶子节点。
代码
1 | # Definition for a binary tree node. |