Engineering platform~20 min2s · 256 MBtarget O(n + m) time, O(n + m) spaceclassic twin: lc-572tree-recursion
You work on the engineering platform behind an app. A page layout is stored as a tree of blocks. Every block has an id and at most two blocks inside it, a left one and a right one.
Design hands you a template: a small block tree. Your team lead asks whether the template appears inside a page layout as a complete section. The shape and every id must match, with nothing extra below.
Write contains_layout(values, left, right, t_values, t_left, t_right).
values, left and right, and the template in t_values, t_left and t_right. In each, blocks are numbered 0 to n - 1, and block 0 is the top.values[i] is the id of block i. left[i] and right[i] are the numbers of the blocks inside it, or -1 where there is none.true when some section of the layout has the same shape as the template and the same id in every place. Return false otherwise. The layout can be empty. The template has at least one block.Input: values = [3, 4, 5, 1, 2], left = [1, 3, -1, -1, -1], right = [2, 4, -1, -1, -1], t_values = [4, 1, 2], t_left = [1, -1, -1], t_right = [2, -1, -1]
Output: trueThe layout has block 0 (id 3) with blocks 1 (id 4) and 2 (id 5) inside it. Block 1 holds 3 (id 1) and 4 (id 2). The template is the id 4 with 1 and 2 inside it.
Give every different (id, left number, right number) a number, from the bottom up. Use one shared table for the template and the layout. A missing block has the number 0.
| Tree | Block (id) | Left number | Right number | Number given |
|---|---|---|---|---|
| template | 2 (id 2) |
0 | 0 | 1 (new) |
| template | 1 (id 1) |
0 | 0 | 2 (new) |
| template | 0 (id 4) |
2 | 1 | 3 (new) |
| layout | 4 (id 2) |
0 | 0 | 1 (seen) |
| layout | 3 (id 1) |
0 | 0 | 2 (seen) |
| layout | 2 (id 5) |
0 | 0 | 4 (new) |
| layout | 1 (id 4) |
2 | 1 | 3 (seen) |
| layout | 0 (id 3) |
3 | 4 | 5 (new) |
The template's top has the number 3, and layout block 1 has it too. Equal numbers mean equal sections, so the answer is true.
Input: values = [1, 2, 3], left = [1, 2, -1], right = [-1, -1, -1], t_values = [1, 2], t_left = [1, -1], t_right = [-1, -1]
Output: falseThe layout is a chain 1, 2, 3. The template 1, 2 matches its first two blocks, but the section at the top also holds the block 3.
Input: values = [], left = [], right = [], t_values = [7], t_left = [-1], t_right = [-1]
Output: false0 ≤ length of values = length of left = length of right ≤ 10^51 ≤ length of t_values = length of t_left = length of t_right ≤ 10^50 ≤ values[i], t_values[i] ≤ 1000left, right, t_left and t_right is -1 or a block number from 1 to n - 10Both trees can be chains of tens of thousands of blocks. Comparing the template with the section at every block can cost about n times m steps, which is billions on two long chains of equal ids. Calls that wait for each other would also be too deep for Python.
Give each different section a number once, and compare numbers. That is about n plus m steps.