Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 793a151

Browse files
committed
Feat: 104
1 parent 7cba6fd commit 793a151

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
## 题目
2+
3+
* 104. 二叉树的最大深度
4+
5+
给定一个二叉树,找出其最大深度。
6+
7+
二叉树的深度为根节点到最远叶子节点的最长路径上的节点数。
8+
9+
说明: 叶子节点是指没有子节点的节点。
10+
11+
## 思路
12+
13+
14+
15+
## 代码
16+
17+
```php
18+
/**
19+
* Definition for a binary tree node.
20+
* class TreeNode {
21+
* public $val = null;
22+
* public $left = null;
23+
* public $right = null;
24+
* function __construct($val = 0, $left = null, $right = null) {
25+
* $this->val = $val;
26+
* $this->left = $left;
27+
* $this->right = $right;
28+
* }
29+
* }
30+
*/
31+
class Solution {
32+
33+
/**
34+
* @param TreeNode $root
35+
* @return Integer
36+
*/
37+
function maxDepth($root) {
38+
if (!$root) {
39+
return 0;
40+
}
41+
$leftDepth = $this->maxDepth($root->left);
42+
$rightDepth = $this->maxDepth($root->right);
43+
return max($leftDepth, $rightDepth) + 1;
44+
}
45+
}
46+
47+
```
48+

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /