Skip to content

Commit

Permalink
feat: finish 99-recover-tree
Browse files Browse the repository at this point in the history
  • Loading branch information
YanceyOfficial committed Nov 14, 2023
1 parent 6a46604 commit 381a168
Show file tree
Hide file tree
Showing 9 changed files with 200 additions and 5 deletions.
5 changes: 5 additions & 0 deletions leetcode-docs/medium/95-generate-trees.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,8 @@ var generateTrees = function (n) {
return dfs(1, n)
}
```

## 复杂度分析

- 时间复杂度:卡特兰数
- 空间复杂度: 卡特兰数
17 changes: 15 additions & 2 deletions leetcode-docs/medium/96-num-trees.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,25 @@ keywords:

## 题解

这里是题解这里是题解这里是题解这里是题解这里是题解
给定一个有序序列 `1``n`,为了构建出一棵二叉搜索树, 我们可以遍历每个数字 `i`, 将该数字作为树根, 将 `1``i - 1` 序列作为左子树,将 `i + 1``n` 序列作为右子树.
接着我们可以按照同样的方式递归构建左子树和右子树.

```ts
/**
* @param {number} n
* @return {number}
*/
var numTrees = function (n) {}
var numTrees = function (n) {
const dp = new Array(n + 1).fill(0)
dp[0] = 1
dp[1] = 1

for (let i = 2; i <= n; i++) {
for (let j = 1; j <= i; j++) {
dp[i] += dp[j - 1] * dp[i - j]
}
}

return dp[n]
}
```
103 changes: 103 additions & 0 deletions leetcode-docs/medium/99-recover-tree.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
---
id: 99-recover-tree
title: 恢复二叉搜索树
sidebar_label: 99. 恢复二叉搜索树
keywords:
- Tree, BST
---

:::success Tips
题目类型: Tree, BST
:::

## 题目

给你二叉搜索树的根节点 `root`. 该树中的**恰好两个节点**的值被错误地交换. 请在不改变其结构的情况下, 恢复这棵树.

:::note 提示:

- 树上节点的数目在范围 `[2, 1000]`
- `-2³¹ <= Node.val <= 2³¹ - 1`

:::

:::info 示例

![33-search](../../static/img/99-recover-tree-1.jpg)

```ts
输入:root = [1,3,null,null,2]
输出:[3,1,null,null,2]
解释:3 不能是 1 的左孩子,因为 3 > 1 。交换 13 使二叉搜索树有效。
```

![33-search](../../static/img/99-recover-tree-2.jpg)

```ts
输入:root = [3,1,4,null,null,2]
输出:[2,1,4,null,null,3]
解释:2 不能在 3 的右子树中,因为 2 < 3 。交换 23 使二叉搜索树有效。
```

:::

## 题解

1. 由于是 BST, 所以使用中序遍历, 把所有节点存储到 `list`
2. 遍历这个列表 `list`, 由于 BST 的中序遍历应当是从小到大排列的, 所以如果前面的 val 大于了后面的 val, 说明后面这个应当靠前.
3. 找到两个需要交换的, 交换他们的值即可

```ts
/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {TreeNode} root
* @return {void} Do not return anything, modify root in-place instead.
*/
var recoverTree = function (root) {
const list = []
inOrder(root, list)
let x = null
let y = null

for (let i = 0; i < list.length - 1; i++) {
if (list[i].val > list[i + 1].val) {
y = list[i + 1]
if (!x) x = list[i]
}
}

if (x && y) {
const tmp = x.val
x.val = y.val
y.val = tmp
}
}

/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {TreeNode} root
* @param {TreeNode[]} list
* @return {void} Do not return anything, modify root in-place instead.
*/
var inOrder = function (root, list) {
if (!root) return null

inOrder(root.left, list)
list.push(root)
inOrder(root.right, list)
}
```
14 changes: 13 additions & 1 deletion src/leetcode/javascript/medium/96.不同的二叉搜索树.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,17 @@
* @param {number} n
* @return {number}
*/
var numTrees = function (n) {}
var numTrees = function (n) {
const dp = new Array(n + 1).fill(0)
dp[0] = 1
dp[1] = 1

for (let i = 2; i <= n; i++) {
for (let j = 1; j <= i; j++) {
dp[i] += dp[j - 1] * dp[i - j]
}
}

return dp[n]
}
// @lc code=end
60 changes: 60 additions & 0 deletions src/leetcode/javascript/medium/99.恢复二叉搜索树.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* @lc app=leetcode.cn id=99 lang=javascript
*
* [99] 恢复二叉搜索树
*/

// @lc code=start
/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {TreeNode} root
* @return {void} Do not return anything, modify root in-place instead.
*/
var recoverTree = function (root) {
const list = []
inOrder(root, list)
let x = null
let y = null

for (let i = 0; i < list.length - 1; i++) {
if (list[i].val > list[i + 1].val) {
y = list[i + 1]
if (!x) x = list[i]
}
}

if (x && y) {
const tmp = x.val
x.val = y.val
y.val = tmp
}
}

/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {TreeNode} root
* @param {TreeNode[]} list
* @return {void} Do not return anything, modify root in-place instead.
*/
var inOrder = function (root, list) {
if (!root) return null

inOrder(root.left, list)
list.push(root)
inOrder(root.right, list)
}
// @lc code=end
5 changes: 3 additions & 2 deletions src/leetcode/rust/src/medium/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
pub mod question_11;
pub mod question_2;
pub mod question_1143;
pub mod question_12;
pub mod question_120;
Expand All @@ -11,6 +10,7 @@ pub mod question_16;
pub mod question_17;
pub mod question_19;
pub mod question_198;
pub mod question_2;
pub mod question_213;
pub mod question_215;
pub mod question_22;
Expand Down Expand Up @@ -87,4 +87,5 @@ pub mod question_90;
pub mod question_91;
pub mod question_926;
pub mod question_93;
pub mod question_97;
pub mod question_97;
pub mod question_99;
1 change: 1 addition & 0 deletions src/leetcode/rust/src/medium/question_99.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub fn foo() -> () {}
Binary file added static/img/99-recover-tree-1.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added static/img/99-recover-tree-2.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 381a168

Please sign in to comment.