长期做前端,今天面试被问了二叉树的中序遍历,居然想了好久。现在代码如下:
#include<stdio.h>
#include<stdlib.h>
typedef struct node{//二叉树结点数据结构定义;
char data;
struct node *lchild,*rchild;
}BTNode,*BTree;
void createBTree(BTree &T){
char c;
scanf("%c",&c);
if(c=='#'){
T=NULL;
}
else{
BTree t;
t=(BTree)malloc(sizeof(BTNode));
t->data=c;
t->lchild=NULL;
t->rchild=NULL;
T=t;
createBTree(T->lchild);
createBTree(T->rchild);
}
}
void inOrderTra(BTree T){
if(T!=NULL){
inOrderTra(T->lchild);
printf("%c ",T->data);
inOrderTra(T->rchild);
}
}
int main(){
BTree T;
printf("请输入二叉树的先序遍历序列:/n");
createBTree(T);
printf("中序遍历二叉树序列:/n");
inOrderTra(T);
printf("/n");
}
合并已排好序的数组:
void merge(int* num1, int m, int* num2, int n) {
int i = m - 1;
int j = n - 1;
while (i >= 0 && j >= 0)
{
if (nums1[i] > nums2[j])
{
nums1[i + j + 1] = nums1[i];
i --;
}else {
nums1[i + j + 1] = nums2[j];
j --;
}
}
while(j >= 0)
{
nums1[i + j + 1] = nums[j];
j--;
}
}
提示:将数组1扩容为 两个数组长度之和。分别从数组尾端开始比较,将大的数字取出,放入扩容数组的尾端,然后变量--。