-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path111.二叉树的最小深度.php
88 lines (85 loc) · 1.72 KB
/
111.二叉树的最小深度.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
<?php
/*
* @lc app=leetcode.cn id=111 lang=php
*
* [111] 二叉树的最小深度
*
* https://leetcode-cn.com/problems/minimum-depth-of-binary-tree/description/
*
* algorithms
* Easy (47.44%)
* Likes: 506
* Dislikes: 0
* Total Accepted: 218.9K
* Total Submissions: 459.5K
* Testcase Example: '[3,9,20,null,null,15,7]'
*
* 给定一个二叉树,找出其最小深度。
*
* 最小深度是从根节点到最近叶子节点的最短路径上的节点数量。
*
* 说明:叶子节点是指没有子节点的节点。
*
*
*
* 示例 1:
*
*
* 输入:root = [3,9,20,null,null,15,7]
* 输出:2
*
*
* 示例 2:
*
*
* 输入:root = [2,null,3,null,4,null,5,null,6]
* 输出:5
*
*
*
*
* 提示:
*
*
* 树中节点数的范围在 [0, 10^5] 内
* -1000
*
*
*/
// @lc code=start
/**
* 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 int
*/
public function minDepth($root)
{
if (! $root) {
return 0;
}
if (! $root->left && ! $root->right) {
return 1;
} elseif ($root->left && ! $root->right) {
return $this->minDepth($root->left) + 1;
} elseif (! $root->left && $root->right) {
return $this->minDepth($root->right) + 1;
} else {
return min($this->minDepth($root->left), $this->minDepth($root->right)) + 1;
}
}
}
// @lc code=end