mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-10 04:06:51 +08:00
添加 层序遍历中的 104. 二叉树的最大深度 Python版本
This commit is contained in:
@ -1532,6 +1532,29 @@ Java:
|
|||||||
|
|
||||||
|
|
||||||
Python:
|
Python:
|
||||||
|
```python 3
|
||||||
|
class Solution:
|
||||||
|
def maxDepth(self, root: TreeNode) -> int:
|
||||||
|
if root == None:
|
||||||
|
return 0
|
||||||
|
|
||||||
|
queue_ = [root]
|
||||||
|
result = []
|
||||||
|
while queue_:
|
||||||
|
length = len(queue_)
|
||||||
|
sub = []
|
||||||
|
for i in range(length):
|
||||||
|
cur = queue_.pop(0)
|
||||||
|
sub.append(cur.val)
|
||||||
|
#子节点入队列
|
||||||
|
if cur.left: queue_.append(cur.left)
|
||||||
|
if cur.right: queue_.append(cur.right)
|
||||||
|
result.append(sub)
|
||||||
|
|
||||||
|
|
||||||
|
return len(result)
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
Go:
|
Go:
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user