Conditions | 8 |
Paths | 14 |
Total Lines | 28 |
Code Lines | 17 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
1 | <?php |
||
11 | public static function largestValues(?TreeNode $tree): array |
||
12 | { |
||
13 | if (!$tree) { |
||
14 | return []; |
||
15 | } |
||
16 | |||
17 | $ans = $queue = []; |
||
|
|||
18 | $queue = [$tree]; |
||
19 | while ($queue) { |
||
20 | [$curr, $n] = [[], count($queue)]; |
||
21 | for ($i = 0; $i < $n; $i++) { |
||
22 | $node = array_shift($queue); |
||
23 | if ($node instanceof TreeNode) { |
||
24 | array_push($curr, $node->val); |
||
25 | if ($node->left) { |
||
26 | array_push($queue, $node->left); |
||
27 | } |
||
28 | if ($node->right) { |
||
29 | array_push($queue, $node->right); |
||
30 | } |
||
31 | } |
||
32 | } |
||
33 | if ($curr) { |
||
34 | array_push($ans, max($curr)); |
||
35 | } |
||
36 | } |
||
37 | |||
38 | return $ans; |
||
39 | } |
||
65 |