二叉树(C语言,手撕)

1. 手撕二叉树

  1. 创建节点:createNode
  2. 增加节点:addNode
  3. 深度优先遍历(递归写法):preOrder、midOrder、postOrder
  4. 深度优先遍历(迭代写法):preOrderByStack、midOrderByStack、postOrderByStack
  5. 广度优先遍历:layerOrderByQueue
  6. 二叉树最大深度:getTreeDepth
  7. 二叉树直径:getTreeDiameter
c 复制代码
#include <stdio.h>
#include <stdlib.h>

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

//<==========================创建节点==========================>
TreeNode* createNode(int val) {
	TreeNode* node = (TreeNode*)malloc(sizeof(TreeNode));
	if (!node) return NULL;
	node->left = NULL;
	node->right = NULL;
	node->val = val;
	return node;
}

//<==========================增加节点==========================>
bool addNode(TreeNode* root, TreeNode* left, TreeNode* right) {
	if (!root) return false;
	root->left = left;
	root->right = right;
	return true;
}

//<==========================深度优先遍历:递归法==========================>
void preOrder(TreeNode* root) {
	if (!root) return;
	printf("%d ", root->val);
	preOrder(root->left);
	preOrder(root->right);
}

void midOrder(TreeNode* root) {
	if (!root) return;
	midOrder(root->left);
	printf("%d ", root->val);
	midOrder(root->right);
}

void postOrder(TreeNode* root) {
	if (!root) return;
	postOrder(root->left);
	postOrder(root->right);
	printf("%d ", root->val);
}

//<==========================深度优先遍历:迭代法==========================>
void preOrderByStack(TreeNode* root) {
	TreeNode* p = root;
	TreeNode* stack[100];
	int stack_top = 0;
	while (p || stack_top != 0) {
		while (p) {
			printf("%d ", p->val);
			stack[stack_top++] = p;
			p = p->left;
		}
		p = stack[--stack_top];
		p = p->right;
	}
}

void midOrderByStack(TreeNode* root) {
	TreeNode* p = root;
	TreeNode* stack[100];
	int stack_top = 0;
	while (p || stack_top != 0) {
		while (p) {
			stack[stack_top++] = p;
			p = p->left;
		}
		p = stack[--stack_top];
		printf("%d ", p->val);
		p = p->right;
	}
}

void postOrderByStack(TreeNode* root) {
	TreeNode* p = root;
	TreeNode* temp = NULL;
	TreeNode* stack[100];
	int stack_top = 0;
	while (p || stack_top != 0) {
		while (p) {
			stack[stack_top++] = p;
			p = p->left;
		}
		p = stack[stack_top - 1];
		if (!p->right || p->right == temp) {
			printf("%d ", p->val);
			temp = p;
			p = NULL;
			--stack_top;
		}
		else {
			p = p->right;
		}
	}
}

//<==========================广度优先遍历:队列==========================>
void layerOrderByqueue(TreeNode* root) {
	TreeNode* queue[100];
	int rear = 0;
	int front = 0;
	TreeNode* p = root;
	queue[rear++] = p;
	while (front != rear) {
		p = queue[front++];
		printf("%d ", p->val);
		if (p->left) queue[rear++] = p->left;
		if (p->right) queue[rear++] = p->right;
	}
}

//<==========================最大深度==========================>
int max(int a, int b) {
	return a < b ? b : a;
}

int getTreeDepth(TreeNode* root) {
	if (!root) return 0;
	return max(getTreeDepth(root->left), getTreeDepth(root->right)) + 1;
}

//<==========================二叉树直径==========================>
int maxDiameter = 0;

int depth(TreeNode* root) {
	if (!root) return 0;
	int L = depth(root->left);
	int R = depth(root->right);
	maxDiameter = max(maxDiameter, L + R + 1);
	return max(L, R) + 1;
}

int getTreeDiameter(TreeNode* root) {
	depth(root);
	return maxDiameter-1;
}

2. 测试用例

c 复制代码
#include <stdio.h>
#include <stdlib.h>

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

//<==========================创建节点==========================>
TreeNode* createNode(int val) {
	TreeNode* node = (TreeNode*)malloc(sizeof(TreeNode));
	if (!node) return NULL;
	node->left = NULL;
	node->right = NULL;
	node->val = val;
	return node;
}

//<==========================增加节点==========================>
bool addNode(TreeNode* root, TreeNode* left, TreeNode* right) {
	if (!root) return false;
	root->left = left;
	root->right = right;
	return true;
}

//<==========================深度优先遍历:递归法==========================>
void preOrder(TreeNode* root) {
	if (!root) return;
	printf("%d ", root->val);
	preOrder(root->left);
	preOrder(root->right);
}

void midOrder(TreeNode* root) {
	if (!root) return;
	midOrder(root->left);
	printf("%d ", root->val);
	midOrder(root->right);
}

void postOrder(TreeNode* root) {
	if (!root) return;
	postOrder(root->left);
	postOrder(root->right);
	printf("%d ", root->val);
}

//<==========================深度优先遍历:迭代法==========================>
void preOrderByStack(TreeNode* root) {
	TreeNode* p = root;
	TreeNode* stack[100];
	int stack_top = 0;
	while (p || stack_top != 0) {
		while (p) {
			printf("%d ", p->val);
			stack[stack_top++] = p;
			p = p->left;
		}
		p = stack[--stack_top];
		p = p->right;
	}
}

void midOrderByStack(TreeNode* root) {
	TreeNode* p = root;
	TreeNode* stack[100];
	int stack_top = 0;
	while (p || stack_top != 0) {
		while (p) {
			stack[stack_top++] = p;
			p = p->left;
		}
		p = stack[--stack_top];
		printf("%d ", p->val);
		p = p->right;
	}
}

void postOrderByStack(TreeNode* root) {
	TreeNode* p = root;
	TreeNode* temp = NULL;
	TreeNode* stack[100];
	int stack_top = 0;
	while (p || stack_top != 0) {
		while (p) {
			stack[stack_top++] = p;
			p = p->left;
		}
		p = stack[stack_top - 1];
		if (!p->right || p->right == temp) {
			printf("%d ", p->val);
			temp = p;
			p = NULL;
			--stack_top;
		}
		else {
			p = p->right;
		}
	}
}

//<==========================广度优先遍历:队列==========================>
void layerOrderByqueue(TreeNode* root) {
	TreeNode* queue[100];
	int rear = 0;
	int front = 0;
	TreeNode* p = root;
	queue[rear++] = p;
	while (front != rear) {
		p = queue[front++];
		printf("%d ", p->val);
		if (p->left) queue[rear++] = p->left;
		if (p->right) queue[rear++] = p->right;
	}
}

//<==========================最大深度==========================>
int max(int a, int b) {
	return a < b ? b : a;
}

int getTreeDepth(TreeNode* root) {
	if (!root) return 0;
	return max(getTreeDepth(root->left), getTreeDepth(root->right)) + 1;
}

//<==========================二叉树直径==========================>
int maxDiameter = 0;

int depth(TreeNode* root) {
	if (!root) return 0;
	int L = depth(root->left);
	int R = depth(root->right);
	maxDiameter = max(maxDiameter, L + R + 1);
	return max(L, R) + 1;
}

int getTreeDiameter(TreeNode* root) {
	depth(root);
	return maxDiameter-1;
}

int main(void) {
	TreeNode* root = createNode(1);
	TreeNode* B = createNode(2);
	TreeNode* C = createNode(3);
	TreeNode* D = createNode(4);
	TreeNode* E = createNode(5);
	TreeNode* F = createNode(6);
	TreeNode* G = createNode(7);

	addNode(root, B, C);
	addNode(B, D, E);
	addNode(C, NULL, F);
	addNode(F, G, NULL);

	printf("================深度优先遍历:递归====================\n");
	printf("递归法-先序遍历:\n");
	preOrder(root);
	printf("\n");

	printf("递归法-中序遍历:\n");
	midOrder(root);
	printf("\n");

	printf("递归法-后序遍历:\n");
	postOrder(root);
	printf("\n");

	printf("================深度优先遍历:迭代====================\n");
	printf("迭代法-先序遍历:\n");
	preOrderByStack(root);
	printf("\n");

	printf("迭代法-中序遍历:\n");
	midOrderByStack(root);
	printf("\n");

	printf("迭代法-后序遍历:\n");
	postOrderByStack(root);
	printf("\n");

	printf("================广度优先遍历:迭代====================\n");
	printf("迭代法-广度优先遍历:\n");
	layerOrderByqueue(root);
	printf("\n");

	printf("=================二叉树的最大深度=====================\n");
	printf("二叉树的最大深度:%d\n", getTreeDepth(root));
	printf("\n");

	printf("====================二叉树的直径======================\n");
	printf("二叉树的直径:%d\n", getTreeDiameter(root));
	printf("\n");
}
相关推荐
晴天的雨.9922 小时前
【C++算法】和为s的两个数
开发语言·数据结构·c++·算法
aramae8 小时前
MySQL复合查询(8)
java·c语言·开发语言·后端·算法
荆棘鸟智能9 小时前
城市感知设备怎么统一接入?从多协议网关到设备模型的中间件架构设计
人工智能·算法·边缘计算
无敌贵点大王9 小时前
RTThread学习记录11——RT-Thread 设备模型吃透:UART/ADC/PWM/PIN 到底有什么区别?
c语言·stm32·学习·链表
淡海水10 小时前
13-04-面试-源码级深度追问链
数据结构·unity·面试·c#·游戏引擎·源码·il2cpp
Logic10110 小时前
C语言/数据结构位运算题解:异或XOR找出时尚聚会中的“独特颜色“——只出现一次的数字
c语言·数据结构·数组·位运算·时间复杂度·算法题·异或性质
INGNIGHT13 小时前
270 · 电话号码的字母组合II(Trie)
linux·算法
2401_8390805413 小时前
C++常见八股
数据结构·c++·算法
青山是哪个青山14 小时前
LeetCode 188:买卖股票的最佳时机 IV
算法
暮雨封夕14 小时前
跳表(Skip List)详解:原理、实现、使用场景与实际应用
数据结构