如何用c语言求二叉树的深度

如何用c语言求二叉树的深度

使用C语言求二叉树深度的几种方法包括递归、层序遍历、深度优先搜索。 其中,递归方法是最常用且简单的一种。通过递归求解左右子树的深度,然后取其最大值加1即可获得二叉树的深度。下面我们将详细介绍这些方法,并提供相应的C语言代码示例。

一、递归方法求二叉树深度

1、递归方法的基本思想

递归方法是最常用的求解二叉树深度的方式。其基本思想是:若二叉树为空,则深度为0;否则,二叉树的深度为左子树深度和右子树深度的最大值加1。

2、递归方法的实现步骤

  1. 检查当前节点是否为空:如果为空,则返回0。
  2. 递归求解左右子树的深度:分别递归调用求左子树和右子树的深度。
  3. 返回最大深度加1:比较左子树和右子树的深度,返回较大者加1。

3、递归方法的C语言实现

#include <stdio.h>

#include <stdlib.h>

// 定义二叉树节点结构

struct TreeNode {

int val;

struct TreeNode *left;

struct TreeNode *right;

};

// 递归求二叉树深度的函数

int maxDepth(struct TreeNode* root) {

if (root == NULL) {

return 0;

}

int leftDepth = maxDepth(root->left);

int rightDepth = maxDepth(root->right);

return (leftDepth > rightDepth ? leftDepth : rightDepth) + 1;

}

// 创建新节点的辅助函数

struct TreeNode* newNode(int val) {

struct TreeNode* node = (struct TreeNode*)malloc(sizeof(struct TreeNode));

node->val = val;

node->left = NULL;

node->right = NULL;

return node;

}

// 主函数,测试递归求二叉树深度

int main() {

struct TreeNode* root = newNode(1);

root->left = newNode(2);

root->right = newNode(3);

root->left->left = newNode(4);

root->left->right = newNode(5);

printf("The depth of the binary tree is: %dn", maxDepth(root));

return 0;

}

二、层序遍历方法求二叉树深度

1、层序遍历方法的基本思想

层序遍历(广度优先搜索)可以借助队列来实现。其基本思想是:每遍历完一层节点,深度加1。直到队列为空时,深度即为二叉树的深度。

2、层序遍历方法的实现步骤

  1. 初始化队列:将根节点加入队列。
  2. 循环遍历:每次从队列中取出一个节点,并将其左右子节点加入队列。
  3. 记录层数:每遍历完一层节点,深度加1。

3、层序遍历方法的C语言实现

#include <stdio.h>

#include <stdlib.h>

// 定义二叉树节点结构

struct TreeNode {

int val;

struct TreeNode *left;

struct TreeNode *right;

};

// 定义队列节点结构

struct QueueNode {

struct TreeNode* treeNode;

struct QueueNode* next;

};

// 定义队列结构

struct Queue {

struct QueueNode* front;

struct QueueNode* rear;

};

// 创建新队列节点的辅助函数

struct QueueNode* newQueueNode(struct TreeNode* treeNode) {

struct QueueNode* node = (struct QueueNode*)malloc(sizeof(struct QueueNode));

node->treeNode = treeNode;

node->next = NULL;

return node;

}

// 初始化队列的辅助函数

struct Queue* createQueue() {

struct Queue* queue = (struct Queue*)malloc(sizeof(struct Queue));

queue->front = queue->rear = NULL;

return queue;

}

// 向队列中添加节点的辅助函数

void enqueue(struct Queue* queue, struct TreeNode* treeNode) {

struct QueueNode* node = newQueueNode(treeNode);

if (queue->rear == NULL) {

queue->front = queue->rear = node;

return;

}

queue->rear->next = node;

queue->rear = node;

}

// 从队列中取出节点的辅助函数

struct TreeNode* dequeue(struct Queue* queue) {

if (queue->front == NULL) {

return NULL;

}

struct QueueNode* temp = queue->front;

queue->front = queue->front->next;

if (queue->front == NULL) {

queue->rear = NULL;

}

struct TreeNode* treeNode = temp->treeNode;

free(temp);

return treeNode;

}

// 判断队列是否为空的辅助函数

int isQueueEmpty(struct Queue* queue) {

return queue->front == NULL;

}

// 层序遍历求二叉树深度的函数

int maxDepth(struct TreeNode* root) {

if (root == NULL) {

return 0;

}

struct Queue* queue = createQueue();

enqueue(queue, root);

int depth = 0;

while (!isQueueEmpty(queue)) {

int levelSize = 0;

struct QueueNode* temp = queue->front;

while (temp != NULL) {

levelSize++;

temp = temp->next;

}

for (int i = 0; i < levelSize; i++) {

struct TreeNode* node = dequeue(queue);

if (node->left != NULL) {

enqueue(queue, node->left);

}

if (node->right != NULL) {

enqueue(queue, node->right);

}

}

depth++;

}

return depth;

}

// 创建新节点的辅助函数

struct TreeNode* newNode(int val) {

struct TreeNode* node = (struct TreeNode*)malloc(sizeof(struct TreeNode));

node->val = val;

node->left = NULL;

node->right = NULL;

return node;

}

// 主函数,测试层序遍历求二叉树深度

int main() {

struct TreeNode* root = newNode(1);

root->left = newNode(2);

root->right = newNode(3);

root->left->left = newNode(4);

root->left->right = newNode(5);

printf("The depth of the binary tree is: %dn", maxDepth(root));

return 0;

}

三、深度优先搜索方法求二叉树深度

1、深度优先搜索方法的基本思想

深度优先搜索(DFS)可以使用栈来实现。其基本思想是:从根节点出发,沿着每一条路径走到底,然后回溯,记录路径长度的最大值。

2、深度优先搜索方法的实现步骤

  1. 初始化栈:将根节点和其深度(1)压入栈。
  2. 循环遍历:每次从栈中取出一个节点,并将其左右子节点及其深度压入栈。
  3. 记录最大深度:每次取出节点时,更新最大深度。

3、深度优先搜索方法的C语言实现

#include <stdio.h>

#include <stdlib.h>

// 定义二叉树节点结构

struct TreeNode {

int val;

struct TreeNode *left;

struct TreeNode *right;

};

// 定义栈节点结构

struct StackNode {

struct TreeNode* treeNode;

int depth;

struct StackNode* next;

};

// 定义栈结构

struct Stack {

struct StackNode* top;

};

// 创建新栈节点的辅助函数

struct StackNode* newStackNode(struct TreeNode* treeNode, int depth) {

struct StackNode* node = (struct StackNode*)malloc(sizeof(struct StackNode));

node->treeNode = treeNode;

node->depth = depth;

node->next = NULL;

return node;

}

// 初始化栈的辅助函数

struct Stack* createStack() {

struct Stack* stack = (struct Stack*)malloc(sizeof(struct Stack));

stack->top = NULL;

return stack;

}

// 向栈中添加节点的辅助函数

void push(struct Stack* stack, struct TreeNode* treeNode, int depth) {

struct StackNode* node = newStackNode(treeNode, depth);

node->next = stack->top;

stack->top = node;

}

// 从栈中取出节点的辅助函数

struct StackNode* pop(struct Stack* stack) {

if (stack->top == NULL) {

return NULL;

}

struct StackNode* temp = stack->top;

stack->top = stack->top->next;

return temp;

}

// 判断栈是否为空的辅助函数

int isStackEmpty(struct Stack* stack) {

return stack->top == NULL;

}

// 深度优先搜索求二叉树深度的函数

int maxDepth(struct TreeNode* root) {

if (root == NULL) {

return 0;

}

struct Stack* stack = createStack();

push(stack, root, 1);

int maxDepth = 0;

while (!isStackEmpty(stack)) {

struct StackNode* node = pop(stack);

struct TreeNode* treeNode = node->treeNode;

int depth = node->depth;

if (treeNode != NULL) {

if (depth > maxDepth) {

maxDepth = depth;

}

push(stack, treeNode->left, depth + 1);

push(stack, treeNode->right, depth + 1);

}

free(node);

}

return maxDepth;

}

// 创建新节点的辅助函数

struct TreeNode* newNode(int val) {

struct TreeNode* node = (struct TreeNode*)malloc(sizeof(struct TreeNode));

node->val = val;

node->left = NULL;

node->right = NULL;

return node;

}

// 主函数,测试深度优先搜索求二叉树深度

int main() {

struct TreeNode* root = newNode(1);

root->left = newNode(2);

root->right = newNode(3);

root->left->left = newNode(4);

root->left->right = newNode(5);

printf("The depth of the binary tree is: %dn", maxDepth(root));

return 0;

}

四、总结

通过上述三种方法——递归、层序遍历和深度优先搜索,我们可以高效地求解二叉树的深度。递归方法简单直观,适合初学者;层序遍历方法可以一层一层地计算深度,适合需要按层处理的场景;深度优先搜索方法可以沿着每一条路径进行深度计算,适合需要记录路径的场景。

推荐使用研发项目管理系统PingCode通用项目管理软件Worktile来管理与二叉树相关的开发任务和项目。这些工具可以帮助你更好地跟踪进度、分配任务和协作,提高团队的效率和产出。

相关问答FAQs:

1. 什么是二叉树的深度?
二叉树的深度是指从根节点到最远叶子节点的最长路径上的节点个数。

2. 如何用C语言计算二叉树的深度?
可以使用递归的方式来计算二叉树的深度。具体步骤是:

  • 如果二叉树为空,则深度为0。
  • 如果二叉树不为空,则分别计算左子树和右子树的深度,取较大的值,然后加1,即为二叉树的深度。

3. 有没有其他计算二叉树深度的方法?
除了使用递归的方法外,还可以使用层序遍历的方法来计算二叉树的深度。具体步骤是:

  • 使用队列来辅助遍历二叉树,先将根节点入队。
  • 当队列不为空时,依次取出队列中的节点,将其左右子节点入队。
  • 每次取出一个节点时,将其深度加1,直到队列为空。
  • 最后得到的深度即为二叉树的深度。

文章包含AI辅助创作,作者:Edit1,如若转载,请注明出处:https://docs.pingcode.com/baike/1109157

(0)
Edit1Edit1
免费注册
电话联系

4008001024

微信咨询
微信咨询
返回顶部