Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[pull] master from youngyangyang04:master #312

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
pull merged 20 commits into AlgorithmAndLeetCode:master from youngyangyang04:master
Jul 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
a9039c0
更新 404.左叶子之和 排版格式修复
jinbudaily Jul 21, 2023
f3a3701
更新 513.找树左下角的值 排版格式修复
jinbudaily Jul 21, 2023
bf6c4e7
更新 0112.路径总和 排版格式修复
jinbudaily Jul 21, 2023
fe93239
更新 0106.从中序与后序遍历序列构造二叉树 排版格式修复
jinbudaily Jul 21, 2023
b4eefe9
更新 0654.最大二叉树 排版格式修复
jinbudaily Jul 21, 2023
eadc704
更新 0617.合并二叉树 排版格式修复
jinbudaily Jul 21, 2023
ac239bc
更新 0700.二叉搜索树中的搜索 排版格式修复
jinbudaily Jul 21, 2023
7746734
更新 0098.验证二叉搜索树 排版格式修复
jinbudaily Jul 21, 2023
74bbca2
更新 0530.二叉搜索树的最小绝对差 排版格式修复
jinbudaily Jul 21, 2023
55b85b5
更新 0501.二叉搜索树中的众数 排版格式修复
jinbudaily Jul 23, 2023
3a2490b
更新 0236.二叉树的最近公共祖先 排版格式修复
jinbudaily Jul 23, 2023
07e1ccc
更新 0235.二叉搜索树的最近公共祖先 排版格式修复
jinbudaily Jul 23, 2023
7027116
更新 0701.二叉搜索树中的插入操作 排版格式修复
jinbudaily Jul 23, 2023
0f22ada
更新 0450.删除二叉搜索树中的节点 排版格式修复
jinbudaily Jul 23, 2023
e14e216
更新 0669.修剪二叉搜索树 排版格式修复
jinbudaily Jul 23, 2023
572c089
更新 0108.将有序数组转换为二叉搜索树 排版格式修复
jinbudaily Jul 23, 2023
6bf34cd
更新 0538.将二叉搜索树转换为累加树 排版格式修复
jinbudaily Jul 23, 2023
7539060
更新 二叉树总结篇 排版格式修复
jinbudaily Jul 23, 2023
2b6e851
Merge branch 'master' of github.com:jinbudaily/leetcode-master
jinbudaily Jul 23, 2023
dd04907
Merge pull request #2199 from jinbudaily/master
youngyangyang04 Jul 24, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 15 additions & 14 deletions problems/0098.验证二叉搜索树.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,18 @@

![98.验证二叉搜索树](https://code-thinking-1253855093.file.myqcloud.com/pics/20230310000750.png)

# 视频讲解
## 算法公开课

**《代码随想录》算法视频公开课:[你对二叉搜索树了解的还不够! | LeetCode:98.验证二叉搜索树](https://www.bilibili.com/video/BV18P411n7Q4),相信结合视频在看本篇题解,更有助于大家对本题的理解**。
**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[你对二叉搜索树了解的还不够! | LeetCode:98.验证二叉搜索树](https://www.bilibili.com/video/BV18P411n7Q4),相信结合视频在看本篇题解,更有助于大家对本题的理解**。


# 思路
## 思路

要知道中序遍历下,输出的二叉搜索树节点的数值是有序序列。

有了这个特性,**验证二叉搜索树,就相当于变成了判断一个序列是不是递增的了。**

## 递归法
### 递归法

可以递归中序遍历将二叉搜索树转变成一个数组,代码如下:

Expand Down Expand Up @@ -211,7 +211,7 @@ public:

最后这份代码看上去整洁一些,思路也清晰。

## 迭代法
### 迭代法

可以用迭代法模拟二叉树中序遍历,对前中后序迭代法生疏的同学可以看这两篇[二叉树:听说递归能做的,栈也能做!](https://programmercarl.com/二叉树的迭代遍历.html),[二叉树:前中后序迭代方式统一写法](https://programmercarl.com/二叉树的统一迭代法.html)

Expand Down Expand Up @@ -245,7 +245,7 @@ public:

在[二叉树:二叉搜索树登场!](https://programmercarl.com/0700.二叉搜索树中的搜索.html)中我们分明写出了痛哭流涕的简洁迭代法,怎么在这里不行了呢,因为本题是要验证二叉搜索树啊。

# 总结
## 总结

这道题目是一个简单题,但对于没接触过的同学还是有难度的。

Expand All @@ -254,10 +254,10 @@ public:
只要把基本类型的题目都做过,总结过之后,思路自然就开阔了。


# 其他语言版本
## 其他语言版本


## Java
### Java

```Java
//使用統一迭代法
Expand Down Expand Up @@ -369,7 +369,7 @@ class Solution {
}
```

## Python
### Python

递归法(版本一)利用中序递增性质,转换成数组
```python
Expand Down Expand Up @@ -479,7 +479,7 @@ class Solution:
```


## Go
### Go

```Go
func isValidBST(root *TreeNode) bool {
Expand Down Expand Up @@ -526,7 +526,7 @@ func isValidBST(root *TreeNode) bool {
}
```

## JavaScript
### JavaScript

辅助数组解决

Expand Down Expand Up @@ -595,7 +595,7 @@ var isValidBST = function (root) {
};
```

## TypeScript
### TypeScript

> 辅助数组解决:

Expand Down Expand Up @@ -637,7 +637,7 @@ function isValidBST(root: TreeNode | null): boolean {
};
```

## Scala
### Scala

辅助数组解决:
```scala
Expand Down Expand Up @@ -682,7 +682,7 @@ object Solution {
}
```

## rust
### Rust

递归:

Expand Down Expand Up @@ -735,3 +735,4 @@ impl Solution {
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

40 changes: 18 additions & 22 deletions problems/0106.从中序与后序遍历序列构造二叉树.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@

![106. 从中序与后序遍历序列构造二叉树1](https://code-thinking-1253855093.file.myqcloud.com/pics/20210203154316774.png)

# 视频讲解
## 算法公开课

**《代码随想录》算法视频公开课:[坑很多!来看看你掉过几次坑 | LeetCode:106.从中序与后序遍历序列构造二叉树](https://www.bilibili.com/video/BV1vW4y1i7dn),相信结合视频在看本篇题解,更有助于大家对本题的理解**。
**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[坑很多!来看看你掉过几次坑 | LeetCode:106.从中序与后序遍历序列构造二叉树](https://www.bilibili.com/video/BV1vW4y1i7dn),相信结合视频在看本篇题解,更有助于大家对本题的理解**。


## 思路
Expand Down Expand Up @@ -158,8 +158,6 @@ root->right = traversal(rightInorder, rightPostorder);

完整代码如下:

### C++完整代码

```CPP
class Solution {
private:
Expand Down Expand Up @@ -281,8 +279,6 @@ public:

下面给出用下标索引写出的代码版本:(思路是一样的,只不过不用重复定义vector了,每次用下标索引来分割)

### C++优化版本

```CPP
class Solution {
private:
Expand Down Expand Up @@ -400,8 +396,9 @@ public:
};
```

## 相关题目推荐

# 105.从前序与中序遍历序列构造二叉树
### 105.从前序与中序遍历序列构造二叉树

[力扣题目链接](https://leetcode.cn/problems/construct-binary-tree-from-preorder-and-inorder-traversal/)

Expand All @@ -418,7 +415,7 @@ public:

![105. 从前序与中序遍历序列构造二叉树](https://code-thinking-1253855093.file.myqcloud.com/pics/20210203154626672.png)

## 思路
### 思路

本题和106是一样的道理。

Expand Down Expand Up @@ -547,7 +544,7 @@ public:
};
```

# 思考题
## 思考题

前序和中序可以唯一确定一棵二叉树。

Expand All @@ -569,7 +566,7 @@ tree2 的前序遍历是[1 2 3], 后序遍历是[3 2 1]。

所以前序和后序不能唯一确定一棵二叉树!

# 总结
## 总结

之前我们讲的二叉树题目都是各种遍历二叉树,这次开始构造二叉树了,思路其实比较简单,但是真正代码实现出来并不容易。

Expand All @@ -585,9 +582,9 @@ tree2 的前序遍历是[1 2 3], 后序遍历是[3 2 1]。



# 其他语言版本
## 其他语言版本

## Java
### Java

106.从中序与后序遍历序列构造二叉树

Expand Down Expand Up @@ -688,7 +685,7 @@ class Solution {
}
```

## Python
### Python

105.从前序与中序遍历序列构造二叉树

Expand Down Expand Up @@ -754,7 +751,7 @@ class Solution:
return root
```

## Go
### Go

106 从中序与后序遍历序列构造二叉树

Expand Down Expand Up @@ -833,9 +830,7 @@ func build(pre []int, in []int, root int, l, r int) *TreeNode {
```




## JavaScript
### JavaScript

```javascript
var buildTree = function(inorder, postorder) {
Expand Down Expand Up @@ -863,7 +858,7 @@ var buildTree = function(preorder, inorder) {
};
```

## TypeScript
### TypeScript

> 106.从中序与后序遍历序列构造二叉树

Expand Down Expand Up @@ -969,7 +964,7 @@ function buildTree(preorder: number[], inorder: number[]): TreeNode | null {
};
```

## C
### C

106 从中序与后序遍历序列构造二叉树

Expand Down Expand Up @@ -1047,7 +1042,7 @@ struct TreeNode* buildTree(int* preorder, int preorderSize, int* inorder, int in
}
```

## Swift
### Swift

105 从前序与中序遍历序列构造二叉树

Expand Down Expand Up @@ -1140,7 +1135,7 @@ class Solution_0106 {
}
```

## Scala
### Scala

106 从中序与后序遍历序列构造二叉树

Expand Down Expand Up @@ -1188,7 +1183,7 @@ object Solution {
}
```

## rust
### Rust

106 从中序与后序遍历序列构造二叉树

Expand Down Expand Up @@ -1238,3 +1233,4 @@ impl Solution {
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

31 changes: 16 additions & 15 deletions problems/0108.将有序数组转换为二叉搜索树.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@

![108.将有序数组转换为二叉搜索树](https://code-thinking-1253855093.file.myqcloud.com/pics/20201022164420763.png)

# 算法公开课
## 算法公开课

**《代码随想录》算法视频公开课:[构造平衡二叉搜索树!| LeetCode:108.将有序数组转换为二叉搜索树](https://www.bilibili.com/video/BV1uR4y1X7qL?share_source=copy_web),相信结合视频在看本篇题解,更有助于大家对本题的理解**。
**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[构造平衡二叉搜索树!| LeetCode:108.将有序数组转换为二叉搜索树](https://www.bilibili.com/video/BV1uR4y1X7qL?share_source=copy_web),相信结合视频在看本篇题解,更有助于大家对本题的理解**。

# 思路
## 思路

做这道题目之前大家可以了解一下这几道:

Expand Down Expand Up @@ -71,7 +71,7 @@

**这也是题目中强调答案不是唯一的原因。 理解这一点,这道题目算是理解到位了**。

## 递归
### 递归

递归三部曲:

Expand Down Expand Up @@ -155,7 +155,7 @@ public:
**注意:在调用traversal的时候传入的left和right为什么是0和nums.size() - 1,因为定义的区间为左闭右闭**。


## 迭代法
### 迭代法

迭代法可以通过三个队列来模拟,一个队列放遍历的节点,一个队列放左区间下标,一个队列放右区间下标。

Expand Down Expand Up @@ -203,7 +203,7 @@ public:
};
```

# 总结
## 总结

**在[二叉树:构造二叉树登场!](https://programmercarl.com/0106.从中序与后序遍历序列构造二叉树.html) 和 [二叉树:构造一棵最大的二叉树](https://programmercarl.com/0654.最大二叉树.html)之后,我们顺理成章的应该构造一下二叉搜索树了,一不小心还是一棵平衡二叉搜索树**。

Expand All @@ -216,10 +216,10 @@ public:
最后依然给出迭代的方法,其实就是模拟取中间元素,然后不断分割去构造二叉树的过程。


# 其他语言版本
## 其他语言版本


## Java
### Java

递归: 左闭右开 [left,right)
```Java
Expand Down Expand Up @@ -315,7 +315,7 @@ class Solution {
}
```

## Python
### Python
递归法
```python
class Solution:
Expand Down Expand Up @@ -377,7 +377,7 @@ class Solution:

```

## Go
### Go

递归(隐含回溯)

Expand All @@ -396,7 +396,7 @@ func sortedArrayToBST(nums []int) *TreeNode {
}
```

## JavaScript
### JavaScript
递归

```javascript
Expand Down Expand Up @@ -453,7 +453,7 @@ var sortedArrayToBST = function(nums) {
return root;
};
```
## TypeScript
### TypeScript

```typescript
function sortedArrayToBST(nums: number[]): TreeNode | null {
Expand All @@ -469,7 +469,7 @@ function sortedArrayToBST(nums: number[]): TreeNode | null {
};
```

## C
### C

递归
```c
Expand All @@ -490,7 +490,7 @@ struct TreeNode* sortedArrayToBST(int* nums, int numsSize) {
}
```

## Scala
### Scala

递归:

Expand All @@ -511,7 +511,7 @@ object Solution {
}
```

## rust
### Rust

递归:

Expand All @@ -536,3 +536,4 @@ impl Solution {
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

Loading

AltStyle によって変換されたページ (->オリジナル) /