-
Notifications
You must be signed in to change notification settings - Fork 0
/
104. 二叉树的最大深度.php
61 lines (57 loc) · 1.68 KB
/
104. 二叉树的最大深度.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
55
56
57
58
59
60
61
<?php
/**
* Created by PhpStorm
* User: jtahstu
* Time: 2022/5/23 15:37
* Des: 104. 二叉树的最大深度
* https://leetcode.cn/problems/maximum-depth-of-binary-tree/
* 输入一棵二叉树的根节点,求该树的深度。从根节点到叶节点依次经过的节点(含根、叶节点)形成树的一条路径,最长路径的长度为树的深度。
*/
/**
* 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 maxDepth($root)
{
if (!$root) return 0;
$res = 1;
$nodes = [[$root, 1]];
while ($nodes) { //层序遍历
list($node, $d) = array_shift($nodes);
$res = max($res, $d);
if ($node->left || $node->right) $d++;
if ($node->left) $nodes[] = [$node->left, $d];
if ($node->right) $nodes[] = [$node->right, $d];
}
return $res;
}
}
/**
* 执行用时:12 ms, 在所有 PHP 提交中击败了51.33%的用户
* 内存消耗:20 MB, 在所有 PHP 提交中击败了98.00%的用户
* 通过测试用例:39 / 39
*/
//递归写法也不错
//class Solution {
// public int maxDepth(TreeNode root) {
// if(root == null) return 0;
// int nLeft = maxDepth(root.left);
// int nRight = maxDepth(root.right);
// return nLeft > nRight ? nLeft + 1 : nRight + 1;
// }
//}