#include <stdio.h> #include <stdlib.h> // 二叉树结点的定义 struct TreeNode { int val; struct TreeNode *left; struct TreeNode *right;}; // 创建新结点 struct TreeNode *createNode(int val) { struct TreeNode *node = (struct TreeNode *)malloc(sizeof(struct TreeNode)); node->val = val; node->left = NULL; node->right = NULL; return node;} // 合并两棵二叉树 struct TreeNode *mergeTrees(struct TreeNode *t1, struct TreeNode *t2) { if (!t1 && !t2) { return NULL; } else if (!t1) { return t2; } else if (!t2) { return t1; } struct TreeNode *root = createNode(t1->val + t2->val); root->left = mergeTrees(t1->left, t2->left); root->right = mergeTrees(t1->right, t2->right); return root;} // 层次遍历二叉树 void levelOrder(struct TreeNode *root) { if (!root) { return; } // 创建队列 struct TreeNode **queue = (struct TreeNode **)malloc(sizeof(struct TreeNode *) * 1000); int front = 0, rear = 0; queue[rear++] = root; while (front < rear) { struct TreeNode *node = queue[front++]; printf("%d ", node->val); if (node->left) { queue[rear++] = node->left; } if (node->right) { queue[rear++] = node->right; } } free(queue);}int main() { struct TreeNode *t1 = createNode(1); t1->left = createNode(3); t1->right = createNode(2); t1->left->left = createNode(5); struct TreeNode *t2 = createNode(2); t2->left = createNode(1); t2->right = createNode(3); t2->left->right = createNode(4); t2->right->right = createNode(7); struct TreeNode *root = mergeTrees(t1, t2); printf("合并后的二叉树:"); levelOrder(root); printf("\n"); return 0; }每一行代码都注释
时间: 2024-03-09 10:46:58 浏览: 73
华为OD机试C卷- 二叉树的广度优先遍历(Java & JS & Python & C).md-私信看全套OD代码及解析
好的,下面是每一行代码的注释:
```c
#include <stdio.h>
#include <stdlib.h>
// 二叉树结点的定义
struct TreeNode {
int val;
struct TreeNode *left;
struct TreeNode *right;
};
// 创建新结点
struct TreeNode *createNode(int val) {
struct TreeNode *node = (struct TreeNode *)malloc(sizeof(struct TreeNode));
node->val = val;
node->left = NULL;
node->right = NULL;
return node;
}
// 合并两棵二叉树
struct TreeNode *mergeTrees(struct TreeNode *t1, struct TreeNode *t2) {
if (!t1 && !t2) { // 如果两棵树都为空,则返回空
return NULL;
} else if (!t1) { // 如果第一棵树为空,则返回第二棵树
return t2;
} else if (!t2) { // 如果第二棵树为空,则返回第一棵树
return t1;
}
struct TreeNode *root = createNode(t1->val + t2->val); // 创建新结点,值为两棵树对应结点的和
root->left = mergeTrees(t1->left, t2->left); // 递归合并左子树
root->right = mergeTrees(t1->right, t2->right); // 递归合并右子树
return root;
}
// 层次遍历二叉树
void levelOrder(struct TreeNode *root) {
if (!root) { // 如果根节点为空,则直接返回
return;
}
// 创建队列
struct TreeNode **queue = (struct TreeNode **)malloc(sizeof(struct TreeNode *) * 1000);
int front = 0, rear = 0;
queue[rear++] = root;
while (front < rear) {
struct TreeNode *node = queue[front++]; // 出队
printf("%d ", node->val); // 输出结点的值
if (node->left) { // 如果左子树不为空,则入队
queue[rear++] = node->left;
}
if (node->right) { // 如果右子树不为空,则入队
queue[rear++] = node->right;
}
}
free(queue); // 释放队列内存
}
int main() {
// 创建两棵二叉树
struct TreeNode *t1 = createNode(1);
t1->left = createNode(3);
t1->right = createNode(2);
t1->left->left = createNode(5);
struct TreeNode *t2 = createNode(2);
t2->left = createNode(1);
t2->right = createNode(3);
t2->left->right = createNode(4);
t2->right->right = createNode(7);
// 合并两棵二叉树,并输出结果
struct TreeNode *root = mergeTrees(t1, t2);
printf("合并后的二叉树:");
levelOrder(root);
printf("\n");
return 0;
}
```
阅读全文