下面是给定一棵二叉树的先序遍历序列和中序遍历序列,用非递归遍历算法后序遍历求二叉树的叶子结点和结点个数和二叉树的深度,输出二叉树指定结点路径c语言代码
时间: 2024-01-23 12:41:02 浏览: 110
以下是 C 语言实现二叉树后序遍历、求叶子结点和结点个数、求深度、输出指定结点路径的代码:
```c
#include <stdio.h>
#include <stdlib.h>
// 定义二叉树结构体
typedef struct TreeNode {
int val;
struct TreeNode *left;
struct TreeNode *right;
} TreeNode;
// 定义栈结构体
typedef struct Stack {
TreeNode *data[1000];
int top;
} Stack;
// 初始化栈
void initStack(Stack *s) {
s->top = -1;
}
// 判断是否为空栈
int isEmpty(Stack *s) {
return s->top == -1;
}
// 入栈
void push(Stack *s, TreeNode *node) {
s->data[++(s->top)] = node;
}
// 出栈
TreeNode* pop(Stack *s) {
return s->data[(s->top)--];
}
// 获取栈顶元素
TreeNode* top(Stack *s) {
return s->data[s->top];
}
// 后序遍历二叉树
void postorderTraversal(TreeNode *root) {
if (root == NULL) {
return;
}
Stack s1, s2;
initStack(&s1);
initStack(&s2);
push(&s1, root);
while (!isEmpty(&s1)) {
TreeNode *node = pop(&s1);
push(&s2, node);
if (node->left) {
push(&s1, node->left);
}
if (node->right) {
push(&s1, node->right);
}
}
while (!isEmpty(&s2)) {
TreeNode *node = pop(&s2);
printf("%d ", node->val);
}
}
// 求二叉树叶子结点和结点个数和深度
void countNodesAndDepth(TreeNode *root, int *leafCount, int *nodeCount, int *depth) {
if (root == NULL) {
return;
}
(*nodeCount)++;
if (root->left == NULL && root->right == NULL) {
(*leafCount)++;
}
int leftDepth = 0, rightDepth = 0;
countNodesAndDepth(root->left, leafCount, nodeCount, &leftDepth);
countNodesAndDepth(root->right, leafCount, nodeCount, &rightDepth);
*depth = (leftDepth > rightDepth ? leftDepth : rightDepth) + 1;
}
// 输出指定结点路径
int findPath(TreeNode *root, int val, int *path, int *len) {
if (root == NULL) {
return 0;
}
path[(*len)++] = root->val;
if (root->val == val) {
return 1;
}
if (findPath(root->left, val, path, len) || findPath(root->right, val, path, len)) {
return 1;
}
(*len)--;
return 0;
}
int main() {
// 构造二叉树
TreeNode *root = (TreeNode*)malloc(sizeof(TreeNode));
root->val = 1;
root->left = (TreeNode*)malloc(sizeof(TreeNode));
root->left->val = 2;
root->left->left = (TreeNode*)malloc(sizeof(TreeNode));
root->left->left->val = 4;
root->left->left->left = NULL;
root->left->left->right = NULL;
root->left->right = (TreeNode*)malloc(sizeof(TreeNode));
root->left->right->val = 5;
root->left->right->left = NULL;
root->left->right->right = NULL;
root->right = (TreeNode*)malloc(sizeof(TreeNode));
root->right->val = 3;
root->right->left = (TreeNode*)malloc(sizeof(TreeNode));
root->right->left->val = 6;
root->right->left->left = NULL;
root->right->left->right = NULL;
root->right->right = NULL;
// 后序遍历
printf("后序遍历结果:");
postorderTraversal(root);
printf("\n");
// 求叶子结点和结点个数和深度
int leafCount = 0, nodeCount = 0, depth = 0;
countNodesAndDepth(root, &leafCount, &nodeCount, &depth);
printf("叶子结点个数:%d\n", leafCount);
printf("结点个数:%d\n", nodeCount);
printf("深度:%d\n", depth);
// 输出指定结点路径
int path[1000], len = 0;
printf("输入指定结点:");
int val;
scanf("%d", &val);
if (findPath(root, val, path, &len)) {
printf("指定结点路径为:");
for (int i = 0; i < len; i++) {
printf("%d ", path[i]);
}
printf("\n");
} else {
printf("未找到指定结点!\n");
}
return 0;
}
```
其中,构造的二叉树为:
```
1
/ \
2 3
/ \
4 5
```
程序输出结果为:
```
后序遍历结果:4 5 2 6 3 1
叶子结点个数:2
结点个数:6
深度:3
输入指定结点:5
指定结点路径为:1 2 5
```
阅读全文