501. 二叉搜索樹中的衆數 Find Mode in Binary Search Tree

題目 https://leetcode-cn.com/problems/find-mode-in-binary-search-tree/

方法一:手寫棧

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct TreeNode {
	int val;
	struct TreeNode *left;
	struct TreeNode *right;
};

struct StackNode{
	struct TreeNode *val;
	struct StackNode *next;
};

struct Stack{
	struct StackNode *top;
};

struct Stack *StackCreate(){
	struct Stack *obj = malloc(sizeof(struct Stack));
	obj->top = NULL;
	return obj;
}

void StackPush(struct Stack * obj,struct TreeNode* val){
	struct StackNode *node = malloc(sizeof(struct StackNode));
	node->val = val;
	node->next = NULL;

	if(obj->top == NULL){
		obj->top = node;
	}else{
		node->next = obj->top;
		obj->top = node;
	}
}

void StackPop(struct Stack * obj){
	if(obj->top == NULL)
		return;
	struct StackNode *node;
	node = obj->top;
	obj->top = node->next;
	free(node);
}

struct TreeNode* StackTop(struct Stack *obj){
	if(obj->top == NULL){
		return NULL;
	}
	return obj->top->val;
}

int StackEmpty(struct Stack *obj){
	if(obj->top == NULL){
		return 1;
	}
	return 0;
}

void StackFree(struct Stack *obj){
	while(obj->top != NULL){
		StackPop(obj);
	}
}

int* findMode(struct TreeNode* root, int* returnSize){
	if(root == NULL)
	{
		*returnSize = 0;
		return NULL;
	}

	struct Stack * stack = StackCreate();
	struct TreeNode* p = root;

	int val=0,val_tmp=0;
	int val_count=0,maxCount=0;

	int *maxNumber = NULL,*maxNumber_tmp = NULL;
	int maxNumber_len=0;
	int maxNumber_size = 16;

	maxNumber = malloc(sizeof(int)*maxNumber_size);

	while(1)
	{
		while(p != NULL)
		{
			StackPush(stack,p);
			p = p->left;
		}
		if(StackEmpty(stack))
			break;
		p = StackTop(stack);
		StackPop(stack);
		
		val = p->val;
		if(val_count == 0)//第一個
		{
			val_tmp = val;
			val_count = 1;
		}
		else if(val_tmp == val)//和前一個相等
		{
			val_count++;
		}
		else//和前一個不相等
		{
			if(val_count > maxCount)//最大次數 大
			{
				maxNumber[0] = val_tmp;
				maxNumber_len = 1;
				maxCount = val_count;
			}
			else if(val_count == maxCount)//最大次數 等 則加
			{
				if(maxNumber_len == maxNumber_size)
				{
					maxNumber_size<<=1;
					maxNumber_tmp = maxNumber;
					maxNumber = malloc(sizeof(int)*maxNumber_size);
					memcpy(maxNumber,maxNumber_tmp,sizeof(int)*maxNumber_len);
					free(maxNumber_tmp);
				}
				maxNumber[maxNumber_len++] = val_tmp;
			}

			val_tmp=val;
			val_count = 1;
		}
		
		p = p->right;
	}

	//最後一個
	if(val_count > maxCount)//最大次數 大
	{
		maxNumber[0] = val_tmp;
		maxNumber_len = 1;
		maxCount = val_count;
	}
	else if(val_count == maxCount)//最大次數 等 則加
	{
		if(maxNumber_len == maxNumber_size)
		{
			maxNumber_size<<=1;
			maxNumber_tmp = maxNumber;
			maxNumber = malloc(sizeof(int)*maxNumber_size);
			memcpy(maxNumber,maxNumber_tmp,sizeof(int)*maxNumber_len);
			free(maxNumber_tmp);
		}
		maxNumber[maxNumber_len++] = val_tmp;
	}

	*returnSize = maxNumber_len;
	return maxNumber;
	
}



int main(){
	
	{
		struct TreeNode *head = malloc(sizeof(struct TreeNode));
		head->val = 1;
		head->left = NULL;
		head->right = NULL;

		struct TreeNode *p = malloc(sizeof(struct TreeNode));
		p->val = 2;
		p->left = NULL;
		p->right = NULL;
		head->right = p;

		struct TreeNode *q = malloc(sizeof(struct TreeNode));
		q->val = 2;
		q->left = NULL;
		q->right = NULL;
		p->left = q;


		int *Count;
		int CountLen;
		int i;
		Count = findMode(head,&CountLen);
		for(i=0;i<CountLen;i++)
		{
			printf("%d, ",Count[i]);
		}
		printf("\n");
	}
	
	{
		struct TreeNode *head = malloc(sizeof(struct TreeNode));
		head->val = 1;
		head->left = NULL;
		head->right = NULL;

		struct TreeNode *p = malloc(sizeof(struct TreeNode));
		p->val = 2;
		p->left = NULL;
		p->right = NULL;
		head->right = p;


		int *Count;
		int CountLen;
		int i;
		Count = findMode(head,&CountLen);
		for(i=0;i<CountLen;i++)
		{
			printf("%d, ",Count[i]);
		}
		printf("\n");
	}
}

方法二:管道 不過leetcode不給用,不知道go給不給呢

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/wait.h>

struct TreeNode {
	int val;
	struct TreeNode *left;
	struct TreeNode *right;
};

void search(struct TreeNode* root,int *fd){
	if(root == NULL)
		return;
	search(root->left,fd);
	write(fd[1],&(root->val),sizeof(int));
	search(root->right,fd);
}

int* findMode(struct TreeNode* root, int* returnSize){
	pid_t pid;
	int fd[2];//fd[0]->read fd[1]->write

	pipe(fd);
	pid = fork();
	if(pid == 0)
	{
		close(fd[0]);
		search(root,fd);
		close(fd[1]);
		exit(0);
	}
	else if(pid > 0)
	{
		close(fd[1]);

		int val=0,val_tmp=0;
		int val_count=0,maxCount=0;

		int *maxNumber = NULL,*maxNumber_tmp;
		int maxNumber_len=0;
		int maxNumber_size = 16;

		if(read(fd[0],&val,sizeof(int)) <=0)//第一個
		{
			maxNumber_len = 0;
			maxNumber = NULL;

			goto finish;
		}

		val_tmp = val;
		val_count = 1;

		maxNumber = malloc(sizeof(int)*maxNumber_size);

		while(read(fd[0],&val,sizeof(int)) > 0)
		{
			if(val_tmp == val)//和前一個相等
			{
				val_count++;
			}
			else//和前一個不相等
			{
				if(val_count > maxCount)//最大次數 大
				{
					maxNumber[0] = val_tmp;
					maxNumber_len = 1;
					maxCount = val_count;
				}
				else if(val_count == maxCount)//最大次數 等 則加
				{
					if(maxNumber_len == maxNumber_size)
					{
						maxNumber_size<<=1;
						maxNumber_tmp = maxNumber;
						maxNumber = malloc(sizeof(int)*maxNumber_size);
						memcpy(maxNumber,maxNumber_tmp,sizeof(int)*maxNumber_len);
						free(maxNumber_tmp);
					}
					maxNumber[maxNumber_len++] = val_tmp;
				}

				val_tmp=val;
				val_count = 1;
			}
		}
		//最後一個
		if(val_count > maxCount)//最大次數 大
		{
			maxNumber[0] = val_tmp;
			maxNumber_len = 1;
			maxCount = val_count;
		}
		else if(val_count == maxCount)//最大次數 等 則加
		{
			if(maxNumber_len == maxNumber_size)
			{
				maxNumber_size<<=1;
				maxNumber_tmp = maxNumber;
				maxNumber = malloc(sizeof(int)*maxNumber_size);
				memcpy(maxNumber,maxNumber_tmp,sizeof(int)*maxNumber_len);
				free(maxNumber_tmp);
			}
			maxNumber[maxNumber_len++] = val_tmp;
		}

		finish:
		close(fd[0]);
		waitpid(pid,NULL,0);

		*returnSize = maxNumber_len;
		return maxNumber;
	}

}



int main(){
	{
		struct TreeNode *head = malloc(sizeof(struct TreeNode));
		head->val = 1;
		head->left = NULL;
		head->right = NULL;

		struct TreeNode *p = malloc(sizeof(struct TreeNode));
		p->val = 2;
		p->left = NULL;
		p->right = NULL;
		head->right = p;

		struct TreeNode *q = malloc(sizeof(struct TreeNode));
		q->val = 2;
		q->left = NULL;
		q->right = NULL;
		p->left = q;


		int *Count;
		int CountLen;
		int i;
		Count = findMode(head,&CountLen);
		for(i=0;i<CountLen;i++)
		{
			printf("%d, ",Count[i]);
		}
		printf("\n");
	}
	
	{
		struct TreeNode *head = malloc(sizeof(struct TreeNode));
		head->val = 1;
		head->left = NULL;
		head->right = NULL;

		struct TreeNode *p = malloc(sizeof(struct TreeNode));
		p->val = 2;
		p->left = NULL;
		p->right = NULL;
		head->right = p;


		int *Count;
		int CountLen;
		int i;
		Count = findMode(head,&CountLen);
		for(i=0;i<CountLen;i++)
		{
			printf("%d, ",Count[i]);
		}
		printf("\n");
	}
}

 

 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章