二叉树的深度计算代码C语言
时间: 2023-11-22 20:04:49 浏览: 50
下面是二叉树深度计算的C语言代码实现:
```c
int GetTreeDepth(BiTree T) {
int depth1 = 0;
int depth2 = 0;
if (T == NULL) {
return 0;
} else {
depth1 = GetTreeDepth(T->lchild);
depth2 = GetTreeDepth(T->rchild);
if (depth1 > depth2) {
return depth1 + 1;
} else {
return depth2 + 1;
}
}
}
```
其中,`BiTree`是二叉树的结构体类型,包含左右子树指针和节点数据等信息。函数`GetTreeDepth`的参数是一个二叉树的指针,返回值是该二叉树的深度。函数的实现采用递归的方式,分别计算左右子树的深度,然后取较大值加1即可得到整棵树的深度。
相关问题
二叉树深度代码c语言
以下是二叉树深度的 C 语言代码:
```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;
} else {
int left_depth = maxDepth(root->left);
int right_depth = maxDepth(root->right);
return left_depth > right_depth ? left_depth + 1 : right_depth + 1;
}
}
// 测试
int main() {
// 构造一棵二叉树
struct TreeNode *root = (struct TreeNode*)malloc(sizeof(struct TreeNode));
root->val = 1;
root->left = (struct TreeNode*)malloc(sizeof(struct TreeNode));
root->left->val = 2;
root->left->left = NULL;
root->left->right = NULL;
root->right = (struct TreeNode*)malloc(sizeof(struct TreeNode));
root->right->val = 3;
root->right->left = NULL;
root->right->right = (struct TreeNode*)malloc(sizeof(struct TreeNode));
root->right->right->val = 4;
root->right->right->left = NULL;
root->right->right->right = NULL;
// 计算二叉树深度
int depth = maxDepth(root);
printf("The depth of the binary tree is: %d\n", depth);
return 0;
}
```
这个程序中,我们定义了一个 `TreeNode` 结构体,用于表示二叉树的节点。`maxDepth` 函数是递归计算二叉树深度的函数,它的返回值是左子树和右子树深度中的较大值加 1。在 `main` 函数中,我们构造了一棵二叉树并计算了它的深度。运行程序,输出结果为:
```
The depth of the binary tree is: 3
```
二叉树的最大深度c语言
以下是二叉树最大深度的C语言代码:
```c
#include <stdio.h>
#include <stdlib.h>
// 定义二叉树节点结构体
typedef struct TreeNode {
int val;
struct TreeNode *left;
struct TreeNode *right;
} TreeNode;
// 计算二叉树最大深度的函数
int maxDepth(TreeNode* root) {
if (root == NULL) {
return 0;
} else {
int leftDepth = maxDepth(root->left);
int rightDepth = maxDepth(root->right);
return (leftDepth > rightDepth) ? (leftDepth + 1) : (rightDepth + 1);
}
}
// 创建一个节点并返回
TreeNode* createNode(int val) {
TreeNode* node = (TreeNode*) malloc(sizeof(TreeNode));
node->val = val;
node->left = NULL;
node->right = NULL;
return node;
}
int main() {
// 构造二叉树
TreeNode* root = createNode(3);
root->left = createNode(9);
root->right = createNode(20);
root->right->left = createNode(15);
root->right->right = createNode(7);
// 计算二叉树最大深度并输出
int depth = maxDepth(root);
printf("The maximum depth of the binary tree is: %d\n", depth);
return 0;
}
```
运行结果:
```
The maximum depth of the binary tree is: 3
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)