Update 0102.二叉树的层序遍历.md

This commit is contained in:
jianghongcheng
2023-05-03 20:12:16 -05:00
committed by GitHub
parent 65085c4e9b
commit 6117327718

View File

@ -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