二叉树(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");
}
相关推荐
喵了meme3 小时前
C语言实战4
c语言·开发语言
智者知已应修善业3 小时前
【求中位数】2024-1-23
c语言·c++·经验分享·笔记·算法
地平线开发者4 小时前
PTQ 量化数值范围与优化
算法·自动驾驶
sali-tec4 小时前
C# 基于halcon的视觉工作流-章68 深度学习-对象检测
开发语言·算法·计算机视觉·重构·c#
测试人社区-小明4 小时前
智能弹性伸缩算法在测试环境中的实践与验证
人工智能·测试工具·算法·机器学习·金融·机器人·量子计算
罗西的思考5 小时前
【Agent】MemOS 源码笔记---(5)---记忆分类
人工智能·深度学习·算法
程序员zgh7 小时前
Linux系统常用命令集合
linux·运维·服务器·c语言·开发语言·c++
Bigan(安)7 小时前
【奶茶Beta专项】【LVGL9.4源码分析】09-core-obj_class对象类系统
linux·c语言·mcu·arm·unix
qq_433554548 小时前
C++数位DP
c++·算法·图论
AshinGau8 小时前
Softmax 与 交叉熵损失
神经网络·算法