mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2026-02-02 18:39:09 +08:00
更新 0106.从中序与后序遍历序列构造二叉树 排版格式修复
This commit is contained in:
@@ -29,9 +29,9 @@
|
||||
|
||||

|
||||
|
||||
# 视频讲解
|
||||
## 算法公开课
|
||||
|
||||
**《代码随想录》算法视频公开课:[坑很多!来看看你掉过几次坑 | LeetCode:106.从中序与后序遍历序列构造二叉树](https://www.bilibili.com/video/BV1vW4y1i7dn),相信结合视频在看本篇题解,更有助于大家对本题的理解**。
|
||||
**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[坑很多!来看看你掉过几次坑 | LeetCode:106.从中序与后序遍历序列构造二叉树](https://www.bilibili.com/video/BV1vW4y1i7dn),相信结合视频在看本篇题解,更有助于大家对本题的理解**。
|
||||
|
||||
|
||||
## 思路
|
||||
@@ -158,8 +158,6 @@ root->right = traversal(rightInorder, rightPostorder);
|
||||
|
||||
完整代码如下:
|
||||
|
||||
### C++完整代码
|
||||
|
||||
```CPP
|
||||
class Solution {
|
||||
private:
|
||||
@@ -281,8 +279,6 @@ public:
|
||||
|
||||
下面给出用下标索引写出的代码版本:(思路是一样的,只不过不用重复定义vector了,每次用下标索引来分割)
|
||||
|
||||
### C++优化版本
|
||||
|
||||
```CPP
|
||||
class Solution {
|
||||
private:
|
||||
@@ -400,8 +396,9 @@ public:
|
||||
};
|
||||
```
|
||||
|
||||
## 相关题目推荐
|
||||
|
||||
# 105.从前序与中序遍历序列构造二叉树
|
||||
### 105.从前序与中序遍历序列构造二叉树
|
||||
|
||||
[力扣题目链接](https://leetcode.cn/problems/construct-binary-tree-from-preorder-and-inorder-traversal/)
|
||||
|
||||
@@ -418,7 +415,7 @@ public:
|
||||
|
||||

|
||||
|
||||
## 思路
|
||||
### 思路
|
||||
|
||||
本题和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.从中序与后序遍历序列构造二叉树
|
||||
|
||||
@@ -688,7 +685,7 @@ class Solution {
|
||||
}
|
||||
```
|
||||
|
||||
## Python
|
||||
### Python
|
||||
|
||||
105.从前序与中序遍历序列构造二叉树
|
||||
|
||||
@@ -754,7 +751,7 @@ class Solution:
|
||||
return root
|
||||
```
|
||||
|
||||
## Go
|
||||
### Go
|
||||
|
||||
106 从中序与后序遍历序列构造二叉树
|
||||
|
||||
@@ -833,9 +830,7 @@ func build(pre []int, in []int, root int, l, r int) *TreeNode {
|
||||
```
|
||||
|
||||
|
||||
|
||||
|
||||
## JavaScript
|
||||
### JavaScript
|
||||
|
||||
```javascript
|
||||
var buildTree = function(inorder, postorder) {
|
||||
@@ -863,7 +858,7 @@ var buildTree = function(preorder, inorder) {
|
||||
};
|
||||
```
|
||||
|
||||
## TypeScript
|
||||
### TypeScript
|
||||
|
||||
> 106.从中序与后序遍历序列构造二叉树
|
||||
|
||||
@@ -969,7 +964,7 @@ function buildTree(preorder: number[], inorder: number[]): TreeNode | null {
|
||||
};
|
||||
```
|
||||
|
||||
## C
|
||||
### C
|
||||
|
||||
106 从中序与后序遍历序列构造二叉树
|
||||
|
||||
@@ -1047,7 +1042,7 @@ struct TreeNode* buildTree(int* preorder, int preorderSize, int* inorder, int in
|
||||
}
|
||||
```
|
||||
|
||||
## Swift
|
||||
### Swift
|
||||
|
||||
105 从前序与中序遍历序列构造二叉树
|
||||
|
||||
@@ -1140,7 +1135,7 @@ class Solution_0106 {
|
||||
}
|
||||
```
|
||||
|
||||
## Scala
|
||||
### Scala
|
||||
|
||||
106 从中序与后序遍历序列构造二叉树
|
||||
|
||||
@@ -1188,7 +1183,7 @@ object Solution {
|
||||
}
|
||||
```
|
||||
|
||||
## rust
|
||||
### Rust
|
||||
|
||||
106 从中序与后序遍历序列构造二叉树
|
||||
|
||||
@@ -1238,3 +1233,4 @@ impl Solution {
|
||||
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
||||
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
|
||||
</a>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user