-
Notifications
You must be signed in to change notification settings - Fork 0
/
剑指 Offer 32 - I. 从上到下打印二叉树.php
47 lines (44 loc) · 1.22 KB
/
剑指 Offer 32 - I. 从上到下打印二叉树.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
<?php
/**
* Created by PhpStorm
* User: jtahstu
* Time: 2022/5/11 18:00
* Des: 剑指 Offer 32 - I. 从上到下打印二叉树
* https://leetcode.cn/problems/cong-shang-dao-xia-da-yin-er-cha-shu-lcof/
* 从上到下打印出二叉树的每个节点,同一层的节点按照从左到右的顺序打印。
*/
/**
* Definition for a binary tree node.
* class TreeNode {
* public $val = null;
* public $left = null;
* public $right = null;
* function __construct($value) { $this->val = $value; }
* }
*/
class Solution {
/**
* @param TreeNode $root
* @return Integer[]
*/
function levelOrder($root) {
$items = [];
$nodes = [$root];
while($nodes) { //层序遍历
$node = array_shift($nodes);
$items[] = $node->val;
if ($node->left !== null) {
$nodes[] = $node->left;
}
if ($node->right !== null) {
$nodes[] = $node->right;
}
}
return $items;
}
}
/**
* 执行用时:8 ms, 在所有 PHP 提交中击败了75.68%的用户
* 内存消耗:19.1 MB, 在所有 PHP 提交中击败了45.95%的用户
* 通过测试用例:34 / 34
*/