二叉树(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");
}
相关推荐
黎雁·泠崖23 分钟前
栈与队列实战通关:3道经典OJ题深度解析
c语言·数据结构·leetcode
ytttr8737 小时前
隐马尔可夫模型(HMM)MATLAB实现范例
开发语言·算法·matlab
AlenTech8 小时前
160. 相交链表 - 力扣(LeetCode)
数据结构·leetcode·链表
点云SLAM8 小时前
凸优化(Convex Optimization)理论(1)
人工智能·算法·slam·数学原理·凸优化·数值优化理论·机器人应用
会周易的程序员8 小时前
多模态AI 基于工业级编译技术的PLC数据结构解析与映射工具
数据结构·c++·人工智能·单例模式·信息可视化·架构
jz_ddk8 小时前
[学习] 卫星导航的码相位与载波相位计算
学习·算法·gps·gnss·北斗
放荡不羁的野指针8 小时前
leetcode150题-动态规划
算法·动态规划
sin_hielo8 小时前
leetcode 1161(BFS)
数据结构·算法·leetcode
一起努力啊~8 小时前
算法刷题-二分查找
java·数据结构·算法
水月wwww9 小时前
【算法设计】动态规划
算法·动态规划