-
Notifications
You must be signed in to change notification settings - Fork 0
/
515. 在每个树行中找最大值.php
54 lines (51 loc) · 1.34 KB
/
515. 在每个树行中找最大值.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
<?php
/**
* Created by PhpStorm
* User: jtahstu
* Time: 2022/6/24 10:16
* Desc: 515. 在每个树行中找最大值
* https://leetcode.cn/problems/find-largest-value-in-each-tree-row/
* 给定一棵二叉树的根节点 root ,请找出该二叉树中每一层的最大值。
*/
/**
* Definition for a binary tree node.
* class TreeNode {
* public $val = null;
* public $left = null;
* public $right = null;
* function __construct($val = 0, $left = null, $right = null) {
* $this->val = $val;
* $this->left = $left;
* $this->right = $right;
* }
* }
*/
class Solution
{
/**
* @param TreeNode $root
* @return Integer[]
*/
function largestValues($root)
{
$items = [$root];
$res = [];
while ($items) {
$nodes = [];
$list = [];
foreach ($items as $item) {
$list[] = $item->val;
if ($item->left) $nodes[] = $item->left;
if ($item->right) $nodes[] = $item->right;
}
$res[] = max($list);
$items = $nodes;
}
return $res;
}
}
/**
* 执行用时:16 ms, 在所有 PHP 提交中击败了25.00%的用户
* 内存消耗:21.1 MB, 在所有 PHP 提交中击败了37.50%的用户
* 通过测试用例:78 / 78
*/