From 4b71311621baad2dc356dbe100e608435cc47bd8 Mon Sep 17 00:00:00 2001 From: shuwen Date: Thu, 19 Aug 2021 12:41:16 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20=E5=B1=82=E5=BA=8F?= =?UTF-8?q?=E9=81=8D=E5=8E=86=E4=B8=AD=E7=9A=84=20104.=20=E4=BA=8C?= =?UTF-8?q?=E5=8F=89=E6=A0=91=E7=9A=84=E6=9C=80=E5=A4=A7=E6=B7=B1=E5=BA=A6?= =?UTF-8?q?=20Python=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0102.二叉树的层序遍历.md | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/problems/0102.二叉树的层序遍历.md b/problems/0102.二叉树的层序遍历.md index 0f9b2df6..dc386684 100644 --- a/problems/0102.二叉树的层序遍历.md +++ b/problems/0102.二叉树的层序遍历.md @@ -1532,6 +1532,29 @@ Java: 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: