mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 03:34:02 +08:00
Update 0102.二叉树的层序遍历.md
This commit is contained in:
@ -2664,24 +2664,31 @@ class Solution {
|
|||||||
Python:
|
Python:
|
||||||
|
|
||||||
```python 3
|
```python 3
|
||||||
|
# Definition for a binary tree node.
|
||||||
|
# class TreeNode:
|
||||||
|
# def __init__(self, val=0, left=None, right=None):
|
||||||
|
# self.val = val
|
||||||
|
# self.left = left
|
||||||
|
# self.right = right
|
||||||
class Solution:
|
class Solution:
|
||||||
def maxDepth(self, root: TreeNode) -> int:
|
def maxDepth(self, root: TreeNode) -> int:
|
||||||
if root == None:
|
if not root:
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
queue_ = [root]
|
|
||||||
depth = 0
|
depth = 0
|
||||||
while queue_:
|
queue = collections.deque([root])
|
||||||
length = len(queue_)
|
|
||||||
for i in range(length):
|
while queue:
|
||||||
cur = queue_.pop(0)
|
|
||||||
sub.append(cur.val)
|
|
||||||
#子节点入队列
|
|
||||||
if cur.left: queue_.append(cur.left)
|
|
||||||
if cur.right: queue_.append(cur.right)
|
|
||||||
depth += 1
|
depth += 1
|
||||||
|
for _ in range(len(queue)):
|
||||||
|
node = queue.popleft()
|
||||||
|
if node.left:
|
||||||
|
queue.append(node.left)
|
||||||
|
if node.right:
|
||||||
|
queue.append(node.right)
|
||||||
|
|
||||||
return depth
|
return depth
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
Go:
|
Go:
|
||||||
|
Reference in New Issue
Block a user