| Total Complexity | 13 |
| Total Lines | 53 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
| 1 | <?php |
||
| 9 | class FindLargestValueInEachTreeRow |
||
| 10 | { |
||
| 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 | } |
||
| 40 | |||
| 41 | public static function largestValues2(?TreeNode $tree): array |
||
| 50 | } |
||
| 51 | |||
| 52 | private static function dfs(?TreeNode $node, int $depth, array & $ans): void |
||
| 62 | } |
||
| 63 | } |
||
| 65 |