From bdb0954e32976832b9bd99dd2dcb2744d55e665b Mon Sep 17 00:00:00 2001 From: Steve2020 <841532108@qq.com> Date: Fri, 28 Jan 2022 17:11:11 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=88104.=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?=EF=BC=89=EF=BC=9A=E5=A2=9E=E5=8A=A0typescript=E7=89=88?= =?UTF-8?q?=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0102.二叉树的层序遍历.md | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/problems/0102.二叉树的层序遍历.md b/problems/0102.二叉树的层序遍历.md index a7716d92..9ac23fdf 100644 --- a/problems/0102.二叉树的层序遍历.md +++ b/problems/0102.二叉树的层序遍历.md @@ -2131,7 +2131,28 @@ var maxDepth = function(root) { }; ``` +TypeScript: + +```typescript +function maxDepth(root: TreeNode | null): number { + let helperQueue: TreeNode[] = []; + let resDepth: number = 0; + let tempNode: TreeNode; + if (root !== null) helperQueue.push(root); + while (helperQueue.length > 0) { + resDepth++; + for (let i = 0, length = helperQueue.length; i < length; i++) { + tempNode = helperQueue.shift()!; + if (tempNode.left) helperQueue.push(tempNode.left); + if (tempNode.right) helperQueue.push(tempNode.right); + } + } + return resDepth; +}; +``` + Swift: + ```swift func maxDepth(_ root: TreeNode?) -> Int { guard let root = root else {