栈篇
删除字符串中所有相邻重复项
typedef struct Stack
{
char * a;
int top;
int capacity;
}Stack;
void StackInit(Stack* ps)
{
assert(ps);
ps->a = NULL;
ps->capacity = ps->top = 0;
}
void StackPush(Stack* ps, int data)
{
assert(ps);
if (ps->capacity == ps->top)
{
int newcapcity = ps->capacity == 0 ? 4 : ps->capacity * 2;
char* newnode = (char*)realloc(ps->a,sizeof(char)* newcapcity);
assert(newnode);
ps->a = newnode;
ps->capacity = newcapcity;
}
ps->a[ps->top] = data;
ps->top++;
}
int StackEmpty(Stack* ps)
{
assert(ps);
return ps->top ==0;
}
void StackPop(Stack* ps)
{
assert(ps);
assert(!StackEmpty(ps));
ps->top--;
}
char StackTop(Stack* ps)
{
assert(ps);
assert(!StackEmpty(ps));
return ps->a[ps->top - 1];
}
int StackSize(Stack* ps)
{
assert(ps);
return ps->top;
}
void StackDestroy(Stack* ps)
{
assert(ps);
free(ps->a);
ps->a = NULL;
ps->capacity = ps->top = 0;
}
char* removeDuplicates(char* s) {
Stack st;
StackInit(&st);
while (*s) {
if (StackEmpty(&st) || StackTop(&st) != *s) {
StackPush(&st, *s);
} else {
if(!StackEmpty(&st) && StackTop(&st) == *s) {
StackPop(&st);
}
}
s++;
}
int num=StackSize(&st);
int j=num-1;
char*a=(char*)malloc(sizeof(char)*(num+1));
if(a==NULL)
perror("malloc fail");
while(!StackEmpty(&st))
{
a[j]=StackTop(&st);
StackPop(&st);
j--;
}
a[num]='\0';
return a;
StackDestroy(&st);
free(a);
}
逆波兰表达式求值
typedef struct Stack
{
int * a;
int top;
int capacity;
}Stack;
void StackInit(Stack* ps)
{
assert(ps);
ps->a = NULL;
ps->capacity = ps->top = 0;
}
void StackPush(Stack* ps, int data)
{
assert(ps);
if (ps->capacity == ps->top)
{
int newcapcity = ps->capacity == 0 ? 4 : ps->capacity * 2;
int* newnode = (int*)realloc(ps->a,sizeof(int)* newcapcity);
assert(newnode);
ps->a = newnode;
ps->capacity = newcapcity;
}
ps->a[ps->top] = data;
ps->top++;
}
int StackEmpty(Stack* ps)
{
assert(ps);
return ps->top ==0;
}
void StackPop(Stack* ps)
{
assert(ps);
assert(!StackEmpty(ps));
ps->top--;
}
int StackTop(Stack* ps)
{
assert(ps);
assert(!StackEmpty(ps));
return ps->a[ps->top - 1];
}
int StackSize(Stack* ps)
{
assert(ps);
return ps->top;
}
void StackDestroy(Stack* ps)
{
assert(ps);
free(ps->a);
ps->a = NULL;
ps->capacity = ps->top = 0;
}
int evalRPN(char** tokens, int tokensSize) {
Stack st;
StackInit(&st);
for(int i=0;i<tokensSize;i++)
{char*token=tokens[i];
if(strcmp(tokens[i],"+")==0||strcmp(tokens[i],"-")==0||strcmp(tokens[i],"*")==0||strcmp(tokens[i],"/")==0)
{ int num1= StackTop(&st);
StackPop(&st);
int num2=StackTop(&st);
StackPop(&st);
if (strcmp(tokens[i],"+")==0)StackPush(&st,num2+num1);
if (strcmp(tokens[i],"-")==0)StackPush(&st,num2-num1);
if (strcmp(tokens[i],"*")==0)StackPush(&st,num2*num1);
if (strcmp(tokens[i],"/")==0)StackPush(&st,num2/num1);
}
else
{
StackPush(&st,atoi(token));}
}
return StackTop(&st);
StackDestroy(&st);
}
二叉树篇
单值二叉树
bool isUnivalTree(struct TreeNode* root) {
if(root==NULL)
return true;
else if(root->left&&root->left->val!=root->val)
return false;
else if(root->right&&root->right->val!=root->val)
return false;
return isUnivalTree(root->left)&&isUnivalTree(root->right);
}
对称二叉树
bool _isSymmetric(struct TreeNode* leftroot,struct TreeNode* rightroot) {
if(leftroot==NULL&&rightroot==NULL)
return true;
if(leftroot==NULL||rightroot==NULL)
return false;
if(leftroot->val!=rightroot->val)
return false;
return _isSymmetric(leftroot->left,rightroot->right)&&_isSymmetric(leftroot->right,rightroot->left);
}
bool isSymmetric(struct TreeNode* root) {
return _isSymmetric(root->left,root->right);
}
二叉树前序遍历
int BinaryTreeSize(struct TreeNode* root)
{return root==NULL?0:BinaryTreeSize(root->left)+BinaryTreeSize(root->right)+1;
}
void aapreorderTraversal(struct TreeNode* root,int*a,int*pi)
{if(root==NULL)
return ;
a[(*pi)++]=root->val;
aapreorderTraversal(root->left,a,pi);
aapreorderTraversal(root->right,a,pi);
}
int* preorderTraversal(struct TreeNode* root, int* returnSize) {
*returnSize= BinaryTreeSize(root);
int*a=(int*)malloc(*returnSize*sizeof(int));
int i=0;
aapreorderTraversal(root,a,&i);
return a;
free(a);
}
中序遍历
int BinaryTreeSize(struct TreeNode* root)
{return root==NULL?0:BinaryTreeSize(root->left)+BinaryTreeSize(root->right)+1;
}
void aapreorderTraversal(struct TreeNode* root,int*a,int*pi)
{if(root==NULL)
return ;
aapreorderTraversal(root->left,a,pi);
a[(*pi)++]=root->val;
aapreorderTraversal(root->right,a,pi);
}
int* inorderTraversal(struct TreeNode* root, int* returnSize) {
*returnSize= BinaryTreeSize(root);
int*a=(int*)malloc(*returnSize*sizeof(int));
int i=0;
aapreorderTraversal(root,a,&i);
return a;
free(a);
}
后序遍历
int BinaryTreeSize(struct TreeNode* root)
{return root==NULL?0:BinaryTreeSize(root->left)+BinaryTreeSize(root->right)+1;
}
void aapostorderTraversal(struct TreeNode* root, int*a,int*pi)
{if(root==NULL)
return ;
aapostorderTraversal(root->left,a,pi);
aapostorderTraversal(root->right,a,pi);
a[(*pi)++]=root->val;
}
int* postorderTraversal(struct TreeNode* root, int* returnSize)
{*returnSize=BinaryTreeSize(root);
int *a=(int*)malloc(*returnSize*sizeof(int));
if(a==NULL)
{perror("malloc fail");}
int i=0;
aapostorderTraversal(root, a,&i);
return a;
}
另一个子树
bool isSameTree(struct TreeNode* p, struct TreeNode* q)
{
if(p==NULL&&q==NULL)
return true;
if(p==NULL||q==NULL)
return false;
if(p->val!=q->val)
return false;
return isSameTree(p->left, q->left)&&isSameTree(p->right,q->right);
}
bool isSubtree(struct TreeNode* root, struct TreeNode* subRoot){
if(root==NULL)
return false;
if(isSameTree(root,subRoot ))
return true;
return isSubtree(root->left,subRoot)||isSubtree(root->right,subRoot);
}
二叉树遍历
#include <stdio.h>
#include <stdlib.h>
typedef struct BinaryTreeNode {
char data;
struct BinaryTreeNode* left;
struct BinaryTreeNode* right;
} BTNode;
BTNode* BinaryTreeCreate(char* a, int* pi)
{if(a[*pi]=='#')
{(*pi)++;
return NULL;}
BTNode* root=(BTNode*)malloc(sizeof(BTNode));
if(root==NULL)
perror("malloc fail");
root->data=a[(*pi)++];
root->left=BinaryTreeCreate(a,pi);
root->right=BinaryTreeCreate(a,pi);
return root;
}
void InOrder(BTNode* root)
{if(root==NULL)
return ;
InOrder(root->left);
printf("%c ",root->data);
InOrder(root->right);
}
int main() {
char arr[100];
scanf("%s", arr);
int i=0;
BTNode*bk= BinaryTreeCreate(arr,&i);
InOrder(bk);
}
相同的树
bool isSameTree(struct TreeNode* p, struct TreeNode* q) {
if(p==NULL&&q==NULL)
return true;
if(p==NULL||q==NULL)
return false;
if(p->val!=q->val)
return false;
return isSameTree(p->left, q->left)&& isSameTree(p->right, q->right);
}
翻转二叉树
struct TreeNode* invertTree(struct TreeNode* root) {
if(root==NULL)
return NULL;
struct TreeNode* rootleft=invertTree(root->left);
struct TreeNode* rootright=invertTree(root->right);
root->left=rootright;
root->right=rootleft;
return root;
}