更新 0106.从中序与后序遍历序列构造二叉树 排版格式修复

This commit is contained in:
jinbudaily
2023-07-21 18:00:07 +08:00
parent bf6c4e7f7b
commit fe93239007
2 changed files with 37 additions and 41 deletions

View File

@ -29,9 +29,9 @@
![106. 从中序与后序遍历序列构造二叉树1](https://code-thinking-1253855093.file.myqcloud.com/pics/20210203154316774.png) ![106. 从中序与后序遍历序列构造二叉树1](https://code-thinking-1253855093.file.myqcloud.com/pics/20210203154316774.png)
# 视频讲解 ## 算法公开课
**《代码随想录》算法视频公开课:[坑很多!来看看你掉过几次坑 | LeetCode106.从中序与后序遍历序列构造二叉树](https://www.bilibili.com/video/BV1vW4y1i7dn),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 **[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html)[坑很多!来看看你掉过几次坑 | LeetCode106.从中序与后序遍历序列构造二叉树](https://www.bilibili.com/video/BV1vW4y1i7dn),相信结合视频在看本篇题解,更有助于大家对本题的理解**。
## 思路 ## 思路
@ -158,8 +158,6 @@ root->right = traversal(rightInorder, rightPostorder);
完整代码如下: 完整代码如下:
### C++完整代码
```CPP ```CPP
class Solution { class Solution {
private: private:
@ -281,8 +279,6 @@ public:
下面给出用下标索引写出的代码版本思路是一样的只不过不用重复定义vector了每次用下标索引来分割 下面给出用下标索引写出的代码版本思路是一样的只不过不用重复定义vector了每次用下标索引来分割
### C++优化版本
```CPP ```CPP
class Solution { class Solution {
private: private:
@ -400,8 +396,9 @@ public:
}; };
``` ```
## 相关题目推荐
# 105.从前序与中序遍历序列构造二叉树 ### 105.从前序与中序遍历序列构造二叉树
[力扣题目链接](https://leetcode.cn/problems/construct-binary-tree-from-preorder-and-inorder-traversal/) [力扣题目链接](https://leetcode.cn/problems/construct-binary-tree-from-preorder-and-inorder-traversal/)
@ -418,7 +415,7 @@ public:
![105. 从前序与中序遍历序列构造二叉树](https://code-thinking-1253855093.file.myqcloud.com/pics/20210203154626672.png) ![105. 从前序与中序遍历序列构造二叉树](https://code-thinking-1253855093.file.myqcloud.com/pics/20210203154626672.png)
## 思路 ### 思路
本题和106是一样的道理。 本题和106是一样的道理。
@ -547,7 +544,7 @@ public:
}; };
``` ```
# 思考题 ## 思考题
前序和中序可以唯一确定一棵二叉树。 前序和中序可以唯一确定一棵二叉树。
@ -569,7 +566,7 @@ tree2 的前序遍历是[1 2 3] 后序遍历是[3 2 1]。
所以前序和后序不能唯一确定一棵二叉树! 所以前序和后序不能唯一确定一棵二叉树!
# 总结 ## 总结
之前我们讲的二叉树题目都是各种遍历二叉树,这次开始构造二叉树了,思路其实比较简单,但是真正代码实现出来并不容易。 之前我们讲的二叉树题目都是各种遍历二叉树,这次开始构造二叉树了,思路其实比较简单,但是真正代码实现出来并不容易。
@ -585,9 +582,9 @@ tree2 的前序遍历是[1 2 3] 后序遍历是[3 2 1]。
# 其他语言版本 ## 其他语言版本
## Java ### Java
106.从中序与后序遍历序列构造二叉树 106.从中序与后序遍历序列构造二叉树
@ -688,7 +685,7 @@ class Solution {
} }
``` ```
## Python ### Python
105.从前序与中序遍历序列构造二叉树 105.从前序与中序遍历序列构造二叉树
@ -754,7 +751,7 @@ class Solution:
return root return root
``` ```
## Go ### Go
106 从中序与后序遍历序列构造二叉树 106 从中序与后序遍历序列构造二叉树
@ -833,9 +830,7 @@ func build(pre []int, in []int, root int, l, r int) *TreeNode {
``` ```
### JavaScript
## JavaScript
```javascript ```javascript
var buildTree = function(inorder, postorder) { var buildTree = function(inorder, postorder) {
@ -863,7 +858,7 @@ var buildTree = function(preorder, inorder) {
}; };
``` ```
## TypeScript ### TypeScript
> 106.从中序与后序遍历序列构造二叉树 > 106.从中序与后序遍历序列构造二叉树
@ -969,7 +964,7 @@ function buildTree(preorder: number[], inorder: number[]): TreeNode | null {
}; };
``` ```
## C ### C
106 从中序与后序遍历序列构造二叉树 106 从中序与后序遍历序列构造二叉树
@ -1047,7 +1042,7 @@ struct TreeNode* buildTree(int* preorder, int preorderSize, int* inorder, int in
} }
``` ```
## Swift ### Swift
105 从前序与中序遍历序列构造二叉树 105 从前序与中序遍历序列构造二叉树
@ -1140,7 +1135,7 @@ class Solution_0106 {
} }
``` ```
## Scala ### Scala
106 从中序与后序遍历序列构造二叉树 106 从中序与后序遍历序列构造二叉树
@ -1188,7 +1183,7 @@ object Solution {
} }
``` ```
## rust ### Rust
106 从中序与后序遍历序列构造二叉树 106 从中序与后序遍历序列构造二叉树
@ -1238,3 +1233,4 @@ impl Solution {
<a href="https://programmercarl.com/other/kstar.html" target="_blank"> <a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/> <img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a> </a>

View File

@ -306,7 +306,7 @@ public:
### Java ### Java
#### 0112.路径总和 0112.路径总和
```java ```java
class solution { class solution {
@ -424,7 +424,7 @@ class solution {
} }
``` ```
#### 0113.路径总和-ii 0113.路径总和-ii
```java ```java
class solution { class solution {
@ -533,7 +533,7 @@ class Solution {
### Python ### Python
#### 0112.路径总和 0112.路径总和
(版本一) 递归 (版本一) 递归
```python ```python
@ -620,7 +620,7 @@ class Solution:
#### 0113.路径总和-ii 0113.路径总和-ii
(版本一) 递归 (版本一) 递归
```python ```python
@ -723,7 +723,7 @@ class Solution:
``` ```
### Go ### Go
#### 112. 路径总和 112. 路径总和
```go ```go
//递归法 //递归法
@ -748,7 +748,7 @@ func hasPathSum(root *TreeNode, targetSum int) bool {
} }
``` ```
#### 113. 路径总和 II 113. 路径总和 II
```go ```go
/** /**
@ -790,7 +790,7 @@ func traverse(node *TreeNode, result *[][]int, currPath *[]int, targetSum int) {
### Javascript ### Javascript
#### 0112.路径总和 0112.路径总和
**递归** **递归**
@ -854,7 +854,7 @@ let hasPathSum = function(root, targetSum) {
}; };
``` ```
#### 0113.路径总和-ii 0113.路径总和-ii
**递归** **递归**
@ -954,7 +954,7 @@ let pathSum = function(root, targetSum) {
### TypeScript ### TypeScript
#### 0112.路径总和 0112.路径总和
**递归法:** **递归法:**
@ -1036,7 +1036,7 @@ function hasPathSum(root: TreeNode | null, targetSum: number): boolean {
}; };
``` ```
#### 0112.路径总和 ii 0112.路径总和 ii
**递归法:** **递归法:**
@ -1074,7 +1074,7 @@ function pathSum(root: TreeNode | null, targetSum: number): number[][] {
### Swift ### Swift
#### 0112.路径总和 0112.路径总和
**递归** **递归**
@ -1143,7 +1143,7 @@ func hasPathSum(_ root: TreeNode?, _ targetSum: Int) -> Bool {
} }
``` ```
#### 0113.路径总和 II 0113.路径总和 II
**递归** **递归**
@ -1196,7 +1196,8 @@ func traversal(_ cur: TreeNode?, count: Int) {
### C ### C
#### 0112.路径总和 0112.路径总和
递归法: 递归法:
```c ```c
@ -1254,7 +1255,7 @@ bool hasPathSum(struct TreeNode* root, int targetSum){
} }
``` ```
#### 0113.路径总和 II 0113.路径总和 II
```c ```c
int** ret; int** ret;
@ -1321,7 +1322,7 @@ int** pathSum(struct TreeNode* root, int targetSum, int* returnSize, int** retur
### Scala ### Scala
#### 0112.路径总和 0112.路径总和
**递归:** **递归:**
@ -1371,7 +1372,7 @@ object Solution {
} }
``` ```
#### 0113.路径总和 II 0113.路径总和 II
**递归:** **递归:**
@ -1409,7 +1410,7 @@ object Solution {
### Rust ### Rust
#### 0112.路径总和 0112.路径总和
递归: 递归:
@ -1463,7 +1464,7 @@ impl Solution {
} }
``` ```
#### 0113.路径总和-ii 0113.路径总和-ii
```rust ```rust
impl Solution { impl Solution {
@ -1516,4 +1517,3 @@ impl Solution {
<a href="https://programmercarl.com/other/kstar.html" target="_blank"> <a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/> <img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a> </a>