第6次作业
5.16设计算法以输出每个结点到根结点之间的路径上的所有结点的值。
#include <stdio.h>
#include <stdlib.h>
// 定义二叉树结构体
typedef struct TreeNode {
int val; // 节点的值
struct TreeNode *left; // 左子节点
struct TreeNode *right; // 右子节点
} TreeNode;
// 输出路径的函数
void printPath(TreeNode *root, int path[], int pathLen) {
for (int i = 0; i < pathLen; i++) {
printf("%d ", path[i]);
}
printf("
");
}
// 查找所有路径的函数
void findPaths(TreeNode *root, int path[], int pathLen) {
if (root == NULL) {
return;
}
path[pathLen] = root->val; // 将当前节点的值存入路径数组
pathLen++; // 路径长度加1
if (root->left == NULL && root->right == NULL) {
printPath(root, path, pathLen); // 如果当前节点是叶子节点,输出路径
} else {
findPaths(root->left, path, pathLen); // 递归遍历左子树
findPaths(root->right, path, pathLen); // 递归遍历右子树
}
}
int main() {
// 创建一个简单的二叉树
TreeNode *root = (TreeNode *)malloc(sizeof(TreeNode));
root->val = 1;
root->left = (TreeNode *)malloc(sizeof(TreeNode));
root->left->val = 2;
root->left->left = NULL;
root->left->right = NULL;
root->right = (TreeNode *)malloc(sizeof(TreeNode));
root->right->val = 3;
root->right->left = NULL;
root->right->right = NULL;
int path[100]; // 存储路径的数组
findPaths(root, path, 0); // 调用查找路径的函数
return 0;
}
5.18设计算法将一棵以顺序存储方式存储在数组A[]中的二叉树(已转换为完全二叉树,补充的虚拟结点为值为’#’)转换为二叉链表存储形式。
#include <stdio.h>
#include <stdlib.h>
// 定义二叉树节点结构体
typedef struct TreeNode {
char val;
struct TreeNode* left;
struct TreeNode* right;
} TreeNode;
// 将顺序存储的完全二叉树转换为二叉链表存储形式
TreeNode* convertToBinaryTree(char A[], int size) {
if (size <= 0) {
return NULL;
}
TreeNode* root = NULL;
TreeNode** nodes = (TreeNode**)malloc(size * sizeof(TreeNode*));
for (int i = 0; i < size; i++) {
if (A[i] != '#') {
nodes[i] = (TreeNode*)malloc(sizeof(TreeNode));
nodes[i]->val = A[i];
nodes[i]->left = NULL;
nodes[i]->right = NULL;
if (root == NULL) {
root = nodes[i];
} else {
if (i % 2 == 1) {
nodes[(i - 1) / 2]->left = nodes[i];
} else {
nodes[(i - 2) / 2]->right = nodes[i];
}
}
} else {
nodes[i] = NULL;
}
}
free(nodes);
return root;
}
// 打印二叉树的前序遍历结果
void preorderTraversal(TreeNode* root) {
if (root == NULL) {
return;
}
printf("%c ", root->val);
preorderTraversal(root->left);
preorderTraversal(root->right);
}
int main() {
char A[] = {'A', 'B', '#', 'D', 'E', '#', 'G', 'H', 'I'};
int size = sizeof(A) / sizeof(A[0]);
TreeNode* root = convertToBinaryTree(A, size);
printf("前序遍历结果: ");
preorderTraversal(root);
printf("\n");
return 0;
}
5.19分别设计出先序、中序和后序遍历二叉树的非递归算法。
#include <stdio.h>
#include <stdlib.h>
// 定义二叉树节点结构体
typedef struct TreeNode {
char val;
struct TreeNode* left;
struct TreeNode* right;
} TreeNode;
// 先序遍历非递归算法
void preorderTraversal(TreeNode* root) {
if (root == NULL) {
return;
}
Stack stack;
stack.push(root);
while (!stack.isEmpty()) {
TreeNode* node = stack.pop();
printf("%c ", node->val);
if (node->right != NULL) {
stack.push(node->right);
}
if (node->left != NULL) {
stack.push(node->left);
}
}
}
// 中序遍历非递归算法
void inorderTraversal(TreeNode* root) {
if (root == NULL) {
return;
}
Stack stack;
TreeNode* current = root;
while (!stack.isEmpty() || current != NULL) {
if (current != NULL) {
stack.push(current);
current = current->left;
} else {
TreeNode* node = stack.pop();
printf("%c ", node->val);
current = node->right;
}
}
}
// 后序遍历非递归算法
void postorderTraversal(TreeNode* root) {
if (root == NULL) {
return;
}
Stack stack1, stack2;
stack1.push(root);
while (!stack1.isEmpty()) {
TreeNode* node = stack1.pop();
stack2.push(node);
if (node->left != NULL) {
stack1.push(node->left);
}
if (node->right != NULL) {
stack1.push(node->right);
}
}
while (!stack2.isEmpty()) {
TreeNode* node = stack2.pop();
printf("%c ", node->val);
}
}
再看up的视频 记录一下今天是2024.5.7 /hfgydx/znkxyjs/2023217~~~/wyt