Z32

Flatten Binary Tree to Linked List

Mid / Senior Engineer
Target Time: 25 - 35 Minutes
Type: ARRAY

Problem Statement & Objective

Given the root of a binary tree, flatten the tree into a 'linked list' in-place following pre-order traversal.

Function Signature (Python)

flatten_binary_tree_to_linked(root: list) -> list

Input & Output Contract

Output Rule:[bas_indeks, baglar]. Boş ağaç [-1, []]. Tek düğüm [0, [[-1, -1]]].

System Constraints

0 <= agac.length <= 2000
-100 <= değer <= 100 veya null

Example Execution Walkthrough

agac = [1, 2, 5, 3, 4, null, 6]
İndeksler: 0:1, 1:2, 2:5, 3:3, 4:4, 5:null, 6:6
Preorder indeksleri: 0, 1, 3, 4, 2, 6
bas = 0
baglar[0] = [-1, 1]
baglar[1] = [-1, 3]
baglar[2] = [-1, 6]
baglar[3] = [-1, 4]
baglar[4] = [-1, 2]
baglar[5] = null
baglar[6] = [-1, -1]
Değer listesi [1, 2, 3, 4, 5, 6] bu bağı vermez.

Example Test Scenarios (3)

Example 1
Input:[[1,2,5,3,4,null,6]]
Expected Output:[0,[[-1,1],[-1,3],[-1,6],[-1,4],[-1,2],null,[-1,-1]]]
Example 2: Boş
Input:[[]]
Expected Output:[-1,[]]
Example 3: Tek Düğüm
Input:[[0]]
Expected Output:[0,[[-1,-1]]]
Loading Python Editor...
Input Parameters
[
  [
    1,
    2,
    5,
    3,
    4,
    null,
    6
  ]
]
Expected Output
[
  0,
  [
    [
      -1,
      1
    ],
    [
      -1,
      3
    ],
    [
      -1,
      6
    ],
    [
      -1,
      4
    ],
    [
      -1,
      2
    ],
    null,
    [
      -1,
      -1
    ]
  ]
]