-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6a46604
commit 381a168
Showing
9 changed files
with
200 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -78,3 +78,8 @@ var generateTrees = function (n) { | |
return dfs(1, n) | ||
} | ||
``` | ||
|
||
## 复杂度分析 | ||
|
||
- 时间复杂度:卡特兰数 | ||
- 空间复杂度: 卡特兰数 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 。交换 1 和 3 使二叉搜索树有效。 | ||
``` | ||
|
||
![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 。交换 2 和 3 使二叉搜索树有效。 | ||
``` | ||
|
||
::: | ||
|
||
## 题解 | ||
|
||
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) | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pub fn foo() -> () {} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.