#include <stdio.h>
#include <stdlib.h>
// 定义二叉树节点
struct Node {
int data;
struct Node* left;
struct Node* right;
};
// 创建新节点
struct Node* createNode(int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
if (newNode == NULL) {
printf("Memory allocation failed\n");
exit(EXIT_FAILURE);
}
newNode->data = data;
newNode->left = NULL;
newNode->right = NULL;
return newNode;
}
// 递归计算二叉树的路径长度
int getPathLength(struct Node* root, int depth) {
if (root == NULL) {
return 0; // 空树的路径长度为0
} else {
// 计算左右子树的路径长度
int leftPathLength = getPathLength(root->left, depth + 1);
int rightPathLength = getPathLength(root->right, depth + 1);
// 返回当前节点的深度和左右子树的路径长度之和
return depth + leftPathLength + rightPathLength;
}
}
int main() {
// 构建一个示例二叉树
stru