mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-10 20:40:39 +08:00
Update 0104.二叉树的最大深度.md
This commit is contained in:
@ -452,14 +452,17 @@ class Solution:
|
|||||||
|
|
||||||
递归法:
|
递归法:
|
||||||
```python
|
```python
|
||||||
class solution:
|
class Solution:
|
||||||
def maxdepth(self, root: 'node') -> int:
|
def maxDepth(self, root: 'Node') -> int:
|
||||||
if not root:
|
if not root:
|
||||||
return 0
|
return 0
|
||||||
depth = 0
|
|
||||||
for i in range(len(root.children)):
|
max_depth = 1
|
||||||
depth = max(depth, self.maxdepth(root.children[i]))
|
|
||||||
return depth + 1
|
for child in root.children:
|
||||||
|
max_depth = max(max_depth, self.maxDepth(child) + 1)
|
||||||
|
|
||||||
|
return max_depth
|
||||||
```
|
```
|
||||||
|
|
||||||
迭代法:
|
迭代法:
|
||||||
|
Reference in New Issue
Block a user