mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 08:50:15 +08:00
Update 0102.二叉树的层序遍历.md
This commit is contained in:
@ -2664,24 +2664,31 @@ class Solution {
|
||||
Python:
|
||||
|
||||
```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:
|
||||
def maxDepth(self, root: TreeNode) -> int:
|
||||
if root == None:
|
||||
if not root:
|
||||
return 0
|
||||
|
||||
queue_ = [root]
|
||||
|
||||
depth = 0
|
||||
while queue_:
|
||||
length = len(queue_)
|
||||
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)
|
||||
queue = collections.deque([root])
|
||||
|
||||
while queue:
|
||||
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
|
||||
|
||||
```
|
||||
|
||||
Go:
|
||||
|
Reference in New Issue
Block a user