Skip to content

Commit bed1f5a

Browse files
Merge pull request youngyangyang04#862 from KingArthur0205/remote
添加 0106.从中序与后续遍历构造二叉树.md,0206.翻转链表.md, 0344.反转字符串 以及0226.翻转二叉树.md C语言版本
2 parents 3f95cef + 5793ebc commit bed1f5a

File tree

4 files changed

+172
-6
lines changed

4 files changed

+172
-6
lines changed

problems/0106.从中序与后序遍历序列构造二叉树.md

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -819,6 +819,81 @@ var buildTree = function(preorder, inorder) {
819819
};
820820
```
821821

822+
## C
823+
106 从中序与后序遍历序列构造二叉树
824+
```c
825+
int linearSearch(int* arr, int arrSize, int key) {
826+
int i;
827+
for(i = 0; i < arrSize; i++) {
828+
if(arr[i] == key)
829+
return i;
830+
}
831+
return -1;
832+
}
833+
834+
struct TreeNode* buildTree(int* inorder, int inorderSize, int* postorder, int postorderSize){
835+
//若中序遍历数组中没有元素,则返回NULL
836+
if(!inorderSize)
837+
return NULL;
838+
//创建一个新的结点,将node的val设置为后序遍历的最后一个元素
839+
struct TreeNode* node = (struct TreeNode*)malloc(sizeof(struct TreeNode));
840+
node->val = postorder[postorderSize - 1];
841+
842+
//通过线性查找找到中间结点在中序数组中的位置
843+
int index = linearSearch(inorder, inorderSize, postorder[postorderSize - 1]);
844+
845+
//左子树数组大小为index
846+
//右子树的数组大小为数组大小减index减1(减的1为中间结点)
847+
int rightSize = inorderSize - index - 1;
848+
node->left = buildTree(inorder, index, postorder, index);
849+
node->right = buildTree(inorder + index + 1, rightSize, postorder + index, rightSize);
850+
return node;
851+
}
852+
```
853+
854+
105 从前序与中序遍历序列构造二叉树
855+
```c
856+
struct TreeNode* buildTree(int* preorder, int preorderSize, int* inorder, int inorderSize){
857+
// 递归结束条件:传入的数组大小为0
858+
if(!preorderSize)
859+
return NULL;
860+
861+
// 1.找到前序遍历数组的第一个元素, 创建结点。左右孩子设置为NULL。
862+
int rootValue = preorder[0];
863+
struct TreeNode* root = (struct TreeNode*)malloc(sizeof(struct TreeNode));
864+
root->val = rootValue;
865+
root->left = NULL;
866+
root->right = NULL;
867+
868+
// 2.若前序遍历数组的大小为1,返回该结点
869+
if(preorderSize == 1)
870+
return root;
871+
872+
// 3.根据该结点切割中序遍历数组,将中序遍历数组分割成左右两个数组。算出他们的各自大小
873+
int index;
874+
for(index = 0; index < inorderSize; index++) {
875+
if(inorder[index] == rootValue)
876+
break;
877+
}
878+
int leftNum = index;
879+
int rightNum = inorderSize - index - 1;
880+
881+
int* leftInorder = inorder;
882+
int* rightInorder = inorder + leftNum + 1;
883+
884+
// 4.根据中序遍历数组左右数组的各子大小切割前序遍历数组。也分为左右数组
885+
int* leftPreorder = preorder+1;
886+
int* rightPreorder = preorder + 1 + leftNum;
887+
888+
// 5.递归进入左右数组,将返回的结果作为根结点的左右孩子
889+
root->left = buildTree(leftPreorder, leftNum, leftInorder, leftNum);
890+
root->right = buildTree(rightPreorder, rightNum, rightInorder, rightNum);
891+
892+
// 6.返回根节点
893+
return root;
894+
}
895+
```
896+
822897
-----------------------
823898
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
824899
* B站视频:[代码随想录](https://space.bilibili.com/525438321)

problems/0206.翻转链表.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,45 @@ func reverse(pre: ListNode?, cur: ListNode?) -> ListNode? {
412412
}
413413
```
414414

415+
C:
416+
双指针法:
417+
```c
418+
struct ListNode* reverseList(struct ListNode* head){
419+
//保存cur的下一个结点
420+
struct ListNode* temp;
421+
//pre指针指向前一个当前结点的前一个结点
422+
struct ListNode* pre = NULL;
423+
//用head代替cur,也可以再定义一个cur结点指向head。
424+
while(head) {
425+
//保存下一个结点的位置
426+
temp = head->next;
427+
//翻转操作
428+
head->next = pre;
429+
//更新结点
430+
pre = head;
431+
head = temp;
432+
}
433+
return pre;
434+
}
435+
```
436+
437+
递归法:
438+
```c
439+
struct ListNode* reverse(struct ListNode* pre, struct ListNode* cur) {
440+
if(!cur)
441+
return pre;
442+
struct ListNode* temp = cur->next;
443+
cur->next = pre;
444+
//将cur作为pre传入下一层
445+
//将temp作为cur传入下一层,改变其指针指向当前cur
446+
return reverse(cur, temp);
447+
}
448+
449+
struct ListNode* reverseList(struct ListNode* head){
450+
return reverse(NULL, head);
451+
}
452+
```
453+
415454
-----------------------
416455
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
417456
* B站视频:[代码随想录](https://space.bilibili.com/525438321)

problems/0226.翻转二叉树.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -565,6 +565,51 @@ var invertTree = function(root) {
565565
};
566566
```
567567

568+
C:
569+
递归法
570+
```c
571+
struct TreeNode* invertTree(struct TreeNode* root){
572+
if(!root)
573+
return NULL;
574+
//交换结点的左右孩子(中)
575+
struct TreeNode* temp = root->right;
576+
root->right = root->left;
577+
root->left = temp;
578+
579+
invertTree(root->left);
580+
//右
581+
invertTree(root->right);
582+
return root;
583+
}
584+
```
585+
迭代法:深度优先遍历
586+
```c
587+
struct TreeNode* invertTree(struct TreeNode* root){
588+
if(!root)
589+
return NULL;
590+
//存储结点的栈
591+
struct TreeNode** stack = (struct TreeNode**)malloc(sizeof(struct TreeNode*) * 100);
592+
int stackTop = 0;
593+
//将根节点入栈
594+
stack[stackTop++] = root;
595+
//若栈中还有元素(进行循环)
596+
while(stackTop) {
597+
//取出栈顶元素
598+
struct TreeNode* temp = stack[--stackTop];
599+
//交换结点的左右孩子
600+
struct TreeNode* tempNode = temp->right;
601+
temp->right = temp->left;
602+
temp->left = tempNode;
603+
//若当前结点有左右孩子,将其入栈
604+
if(temp->right)
605+
stack[stackTop++] = temp->right;
606+
if(temp->left)
607+
stack[stackTop++] = temp->left;
608+
}
609+
return root;
610+
}
611+
```
612+
568613
-----------------------
569614
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
570615
* B站视频:[代码随想录](https://space.bilibili.com/525438321)

problems/0344.反转字符串.md

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -218,12 +218,19 @@ func reverseString(_ s: inout [Character]) {
218218
}
219219
}
220220

221-
// 双指针法 - 库函数
222-
func reverseString(_ s: inout [Character]) {
223-
var j = s.count - 1
224-
for i in 0 ..< Int(Double(s.count) * 0.5) {
225-
s.swapAt(i, j)
226-
j -= 1
221+
```
222+
223+
C:
224+
```c
225+
void reverseString(char* s, int sSize){
226+
int left = 0;
227+
int right = sSize - 1;
228+
229+
while(left < right) {
230+
char temp = s[left];
231+
s[left++] = s[right];
232+
s[right--] = temp;
233+
227234
}
228235
}
229236
```

0 commit comments

Comments
 (0)