From 9329ecff5e95e5bc1e8cb17fbba1135765fd3286 Mon Sep 17 00:00:00 2001 From: Steve2020 <841532108@qq.com> Date: Fri, 28 Jan 2022 12:02:15 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=88515.=E5=9C=A8?= =?UTF-8?q?=E6=AF=8F=E4=B8=AA=E6=A0=91=E8=A1=8C=E4=B8=AD=E6=89=BE=E6=9C=80?= =?UTF-8?q?=E5=A4=A7=E5=80=BC=EF=BC=89=EF=BC=9A=E5=A2=9E=E5=8A=A0typescrip?= =?UTF-8?q?t=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0102.二叉树的层序遍历.md | 27 +++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/problems/0102.二叉树的层序遍历.md b/problems/0102.二叉树的层序遍历.md index 5dd448b7..6f90cb75 100644 --- a/problems/0102.二叉树的层序遍历.md +++ b/problems/0102.二叉树的层序遍历.md @@ -1393,7 +1393,34 @@ var largestValues = function(root) { }; ``` +TypeScript: + +```typescript +function largestValues(root: TreeNode | null): number[] { + let helperQueue: TreeNode[] = []; + let resArr: number[] = []; + let tempNode: TreeNode; + let max: number = 0; + if (root !== null) helperQueue.push(root); + while (helperQueue.length > 0) { + for (let i = 0, length = helperQueue.length; i < length; i++) { + tempNode = helperQueue.shift()!; + if (i === 0) { + max = tempNode.val; + } else { + max = max > tempNode.val ? max : tempNode.val; + } + if (tempNode.left) helperQueue.push(tempNode.left); + if (tempNode.right) helperQueue.push(tempNode.right); + } + resArr.push(max); + } + return resArr; +}; +``` + Swift: + ```swift func largestValues(_ root: TreeNode?) -> [Int] { var res = [Int]()