使用c语言实现统计二叉树的深度
时间: 2024-01-23 10:40:10 浏览: 28
可以使用递归的方式实现统计二叉树的深度,具体实现如下:
```c
// 定义二叉树结构体
typedef struct TreeNode {
int val;
struct TreeNode *left;
struct TreeNode *right;
} TreeNode;
// 计算二叉树深度的函数
int maxDepth(TreeNode* root) {
if (root == NULL) { // 如果为空树,深度为0
return 0;
} else {
int leftDepth = maxDepth(root->left); // 递归计算左子树深度
int rightDepth = maxDepth(root->right); // 递归计算右子树深度
return (leftDepth > rightDepth) ? (leftDepth + 1) : (rightDepth + 1); // 返回左右子树深度的最大值加1
}
}
```
在这个函数中,如果二叉树为空树,则深度为0;否则,递归地计算左子树和右子树的深度,并返回左右子树深度的最大值加1,即为整棵树的深度。
相关问题
用C语言写一个算法统计二叉树的深度
您好,以下是回答您的问题:
可以使用递归的方式来统计二叉树的深度,具体实现如下:
```
#include <stdio.h>
#include <stdlib.h>
// 定义二叉树结构体
typedef struct TreeNode {
int val;
struct TreeNode *left;
struct TreeNode *right;
} TreeNode;
// 创建二叉树
TreeNode* createTree() {
int val;
scanf("%d", &val);
if (val == -1) {
return NULL;
}
TreeNode *root = (TreeNode*)malloc(sizeof(TreeNode));
root->val = val;
root->left = createTree();
root->right = createTree();
return root;
}
// 统计二叉树深度
int maxDepth(TreeNode* root) {
if (root == NULL) {
return ;
}
int leftDepth = maxDepth(root->left);
int rightDepth = maxDepth(root->right);
return leftDepth > rightDepth ? leftDepth + 1 : rightDepth + 1;
}
int main() {
printf("请输入二叉树的节点值,-1表示空节点:\n");
TreeNode *root = createTree();
int depth = maxDepth(root);
printf("二叉树的深度为:%d\n", depth);
return ;
}
```
以上是用 C 语言写一个算法统计二叉树的深度的实现方法,希望能对您有所帮助。
使用c语言编写代码实现二叉树的以下功能:二叉树的创建、二叉树的三种递归遍历、二叉树的复制、统计二叉树的结点数,统计二叉树叶子节点数、计算二叉树的深度、交换二叉树每个结点的左孩和右孩。
以下是使用C语言实现二叉树的各种功能的代码:
1. 二叉树的创建
```c
#include <stdio.h>
#include <stdlib.h>
struct TreeNode {
int val;
struct TreeNode *left;
struct TreeNode *right;
};
struct TreeNode* createTree() {
int val;
scanf("%d", &val);
if (val == -1) {
return NULL;
}
struct TreeNode *node = (struct TreeNode*)malloc(sizeof(struct TreeNode));
node->val = val;
node->left = createTree();
node->right = createTree();
return node;
}
```
2. 二叉树的三种递归遍历
```c
void preorderTraversal(struct TreeNode* root) {
if (root == NULL) {
return;
}
printf("%d ", root->val);
preorderTraversal(root->left);
preorderTraversal(root->right);
}
void inorderTraversal(struct TreeNode* root) {
if (root == NULL) {
return;
}
inorderTraversal(root->left);
printf("%d ", root->val);
inorderTraversal(root->right);
}
void postorderTraversal(struct TreeNode* root) {
if (root == NULL) {
return;
}
postorderTraversal(root->left);
postorderTraversal(root->right);
printf("%d ", root->val);
}
```
3. 二叉树的复制
```c
struct TreeNode* cloneTree(struct TreeNode* root) {
if (root == NULL) {
return NULL;
}
struct TreeNode *node = (struct TreeNode*)malloc(sizeof(struct TreeNode));
node->val = root->val;
node->left = cloneTree(root->left);
node->right = cloneTree(root->right);
return node;
}
```
4. 统计二叉树的结点数
```c
int countNodes(struct TreeNode* root) {
if (root == NULL) {
return 0;
}
return 1 + countNodes(root->left) + countNodes(root->right);
}
```
5. 统计二叉树叶子节点数
```c
int countLeaves(struct TreeNode* root) {
if (root == NULL) {
return 0;
}
if (root->left == NULL && root->right == NULL) {
return 1;
}
return countLeaves(root->left) + countLeaves(root->right);
}
```
6. 计算二叉树的深度
```c
int maxDepth(struct TreeNode* root) {
if (root == NULL) {
return 0;
}
int leftDepth = maxDepth(root->left);
int rightDepth = maxDepth(root->right);
return leftDepth > rightDepth ? leftDepth + 1 : rightDepth + 1;
}
```
7. 交换二叉树每个结点的左孩和右孩
```c
void swapChildren(struct TreeNode* root) {
if (root == NULL) {
return;
}
struct TreeNode *temp = root->left;
root->left = root->right;
root->right = temp;
swapChildren(root->left);
swapChildren(root->right);
}
```
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)