Conditions | 6 |
Paths | 7 |
Total Lines | 26 |
Code Lines | 17 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
1 | <?php |
||
57 | public static function fromArray(array $array): ?TreeNode |
||
58 | { |
||
59 | if (empty($array)) { |
||
60 | return null; |
||
61 | } |
||
62 | [$queue, $root] = [[], new TreeNode($array[0])]; |
||
63 | [$i, $n] = [0, count($array)]; |
||
64 | $queue[] = $root; |
||
65 | |||
66 | while ($i < $n) { |
||
67 | $node = array_shift($queue); |
||
68 | if ($node) { |
||
69 | if (isset($array[++$i])) { |
||
70 | $value = $array[$i]; |
||
71 | $node->left = new TreeNode($value); |
||
72 | $queue[] = $node->left; |
||
73 | } |
||
74 | if (isset($array[++$i])) { |
||
75 | $value = $array[$i]; |
||
76 | $node->right = new TreeNode($value); |
||
77 | $queue[] = $node->right; |
||
78 | } |
||
79 | } |
||
80 | } |
||
81 | |||
82 | return $root; |
||
83 | } |
||
85 |