添加 0106.从中序与后续遍历构造二叉树.md C语言版本

This commit is contained in:
Arthur
2021-10-20 16:10:04 +01:00
parent 29b5373e11
commit 5cb33c319b

View File

@ -818,6 +818,81 @@ var buildTree = function(preorder, inorder) {
};
```
## C
106 从中序与后序遍历序列构造二叉树
```c
int linearSearch(int* arr, int arrSize, int key) {
int i;
for(i = 0; i < arrSize; i++) {
if(arr[i] == key)
return i;
}
return -1;
}
struct TreeNode* buildTree(int* inorder, int inorderSize, int* postorder, int postorderSize){
//若中序遍历数组中没有元素则返回NULL
if(!inorderSize)
return NULL;
//创建一个新的结点将node的val设置为后序遍历的最后一个元素
struct TreeNode* node = (struct TreeNode*)malloc(sizeof(struct TreeNode));
node->val = postorder[postorderSize - 1];
//通过线性查找找到中间结点在中序数组中的位置
int index = linearSearch(inorder, inorderSize, postorder[postorderSize - 1]);
//左子树数组大小为index
//右子树的数组大小为数组大小减index减1减的1为中间结点
int rightSize = inorderSize - index - 1;
node->left = buildTree(inorder, index, postorder, index);
node->right = buildTree(inorder + index + 1, rightSize, postorder + index, rightSize);
return node;
}
```
105 从前序与中序遍历序列构造二叉树
```c
struct TreeNode* buildTree(int* preorder, int preorderSize, int* inorder, int inorderSize){
// 递归结束条件传入的数组大小为0
if(!preorderSize)
return NULL;
// 1.找到前序遍历数组的第一个元素, 创建结点。左右孩子设置为NULL。
int rootValue = preorder[0];
struct TreeNode* root = (struct TreeNode*)malloc(sizeof(struct TreeNode));
root->val = rootValue;
root->left = NULL;
root->right = NULL;
// 2.若前序遍历数组的大小为1返回该结点
if(preorderSize == 1)
return root;
// 3.根据该结点切割中序遍历数组,将中序遍历数组分割成左右两个数组。算出他们的各自大小
int index;
for(index = 0; index < inorderSize; index++) {
if(inorder[index] == rootValue)
break;
}
int leftNum = index;
int rightNum = inorderSize - index - 1;
int* leftInorder = inorder;
int* rightInorder = inorder + leftNum + 1;
// 4.根据中序遍历数组左右数组的各子大小切割前序遍历数组。也分为左右数组
int* leftPreorder = preorder+1;
int* rightPreorder = preorder + 1 + leftNum;
// 5.递归进入左右数组,将返回的结果作为根结点的左右孩子
root->left = buildTree(leftPreorder, leftNum, leftInorder, leftNum);
root->right = buildTree(rightPreorder, rightNum, rightInorder, rightNum);
// 6.返回根节点
return root;
}
```
-----------------------
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
* B站视频[代码随想录](https://space.bilibili.com/525438321)