From 4ed65b50d174a3d9f89d0352ee30da95fcecece9 Mon Sep 17 00:00:00 2001 From: Steve2020 <841532108@qq.com> Date: Wed, 9 Feb 2022 22:45:31 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880513.=E6=89=BE?= =?UTF-8?q?=E6=A0=91=E5=B7=A6=E4=B8=8B=E8=A7=92=E7=9A=84=E5=80=BC.md?= =?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/0513.找树左下角的值.md | 45 ++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/problems/0513.找树左下角的值.md b/problems/0513.找树左下角的值.md index 84ed3932..12c62c70 100644 --- a/problems/0513.找树左下角的值.md +++ b/problems/0513.找树左下角的值.md @@ -433,6 +433,51 @@ var findBottomLeftValue = function(root) { }; ``` +## TypeScript + +> 递归法: + +```typescript +function findBottomLeftValue(root: TreeNode | null): number { + function recur(root: TreeNode, depth: number): void { + if (root.left === null && root.right === null) { + if (depth > maxDepth) { + maxDepth = depth; + resVal = root.val; + } + return; + } + if (root.left !== null) recur(root.left, depth + 1); + if (root.right !== null) recur(root.right, depth + 1); + } + let maxDepth: number = 0; + let resVal: number = 0; + if (root === null) return resVal; + recur(root, 1); + return resVal; +}; +``` + +> 迭代法: + +```typescript +function findBottomLeftValue(root: TreeNode | null): number { + let helperQueue: TreeNode[] = []; + if (root !== null) helperQueue.push(root); + let resVal: number = 0; + let tempNode: TreeNode; + while (helperQueue.length > 0) { + resVal = helperQueue[0].val; + for (let i = 0, length = helperQueue.length; i < length; i++) { + tempNode = helperQueue.shift()!; + if (tempNode.left !== null) helperQueue.push(tempNode.left); + if (tempNode.right !== null) helperQueue.push(tempNode.right); + } + } + return resVal; +}; +``` + ## Swift 递归版本: From 7d8e9e8fae9d580013b717ff882bd9a942fe0e4f Mon Sep 17 00:00:00 2001 From: Guang-Hou <87743934+Guang-Hou@users.noreply.github.com> Date: Wed, 9 Feb 2022 16:37:44 -0500 Subject: [PATCH 2/3] =?UTF-8?q?Update=200701.=E4=BA=8C=E5=8F=89=E6=90=9C?= =?UTF-8?q?=E7=B4=A2=E6=A0=91=E4=B8=AD=E7=9A=84=E6=8F=92=E5=85=A5=E6=93=8D?= =?UTF-8?q?=E4=BD=9C.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Python 递归法- 无返回值的一种更简单的实现。 --- .../0701.二叉搜索树中的插入操作.md | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/problems/0701.二叉搜索树中的插入操作.md b/problems/0701.二叉搜索树中的插入操作.md index 468f2675..5e9fbdfe 100644 --- a/problems/0701.二叉搜索树中的插入操作.md +++ b/problems/0701.二叉搜索树中的插入操作.md @@ -310,6 +310,26 @@ class Solution: return root ``` +**递归法** - 无返回值 - another easier way +```python +class Solution: + def insertIntoBST(self, root: Optional[TreeNode], val: int) -> Optional[TreeNode]: + newNode = TreeNode(val) + if not root: return newNode + + if not root.left and val < root.val: + root.left = newNode + if not root.right and val > root.val: + root.right = newNode + + if val < root.val: + self.insertIntoBST(root.left, val) + if val > root.val: + self.insertIntoBST(root.right, val) + + return root +``` + **迭代法** 与无返回值的递归函数的思路大体一致 ```python From fc1a7e3e079958e6919bbc4fbe069a8d92e66322 Mon Sep 17 00:00:00 2001 From: zhaoninge Date: Thu, 10 Feb 2022 17:01:31 +0800 Subject: [PATCH 3/3] =?UTF-8?q?Update=200027.=E7=A7=BB=E9=99=A4=E5=85=83?= =?UTF-8?q?=E7=B4=A0.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 添加27. 移除元素 C++版相向双指针方法,该方法基于元素顺序可以改变的题目描述,使用双指针法改变了元素相对位置,确保了移动最少元素 --- problems/0027.移除元素.md | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/problems/0027.移除元素.md b/problems/0027.移除元素.md index d69f2bcf..0c6f5129 100644 --- a/problems/0027.移除元素.md +++ b/problems/0027.移除元素.md @@ -106,6 +106,37 @@ public: 旧文链接:[数组:就移除个元素很难么?](https://programmercarl.com/0027.移除元素.html) +```CPP +/** +* 相向双指针方法,基于元素顺序可以改变的题目描述改变了元素相对位置,确保了移动最少元素 +* 时间复杂度:$O(n)$ +* 空间复杂度:$O(1)$ +*/ +class Solution { +public: + int removeElement(vector& nums, int val) { + int leftIndex = 0; + int rightIndex = nums.size() - 1; + while (leftIndex <= rightIndex) { + // 找左边等于val的元素 + while (leftIndex <= rightIndex && nums[leftIndex] != val){ + ++leftIndex; + } + // 找右边不等于val的元素 + while (leftIndex <= rightIndex && nums[rightIndex] == val) { + -- rightIndex; + } + // 将右边不等于val的元素覆盖左边等于val的元素 + if (leftIndex < rightIndex) { + nums[leftIndex++] = nums[rightIndex--]; + } + } + return leftIndex; // leftIndex一定指向了最终数组末尾的下一个元素 + } +}; +``` + + ## 相关题目推荐 * 26.删除排序数组中的重复项