1364:二叉树遍历(flist)

【算法分析】

递归 构造子树的中序遍历序列和层次遍历序列
       层次遍历序列第一个元素,一定是整棵树的根结点。在中序遍历序列中找到该根结点元素,其左边就是左子树的中序遍历序列,右边就是右子树的中序遍历序列。接下来我们需要构造左右子树的层次遍历序列。

【参考代码】

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
string in,level;//int中序遍历 level层序遍历
void preorder(int l1,int r1,int l2,int r2){
	int pos;
	for(int i=l2;i<=r2;i++){
		int flag=0;
		for(int j=l1;j<=r1;j++){
			if(level[i]==in[j]){//找根
				cout<<in[j];
				pos=j;
				flag=1;
				break;
			}
		}
		if(flag) break;
	}
	if(pos>l1) preorder(l1,pos-1,l2,r2);
	if(pos<r1) preorder(pos+1,r1,l2,r2);
}
int main()
{
	cin>>in>>level;
	preorder(0,in.length()-1,0,level.length()-1);	
    return 0;
}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是基于 C 语言的二叉树遍历和统计叶子节点数和深度的代码: ```c #include <stdio.h> #include <stdlib.h> // 定义二叉树节点结构体 typedef struct TreeNode { char data; struct TreeNode *left; struct TreeNode *right; } TreeNode; // 创建二叉树 TreeNode* createTree() { char c; scanf("%c", &c); if (c == '^') { return NULL; } TreeNode *root = (TreeNode*)malloc(sizeof(TreeNode)); root->data = c; root->left = createTree(); root->right = createTree(); return root; } // 先序遍历 void preOrder(TreeNode *root) { if (root == NULL) { return; } printf("%c ", root->data); preOrder(root->left); preOrder(root->right); } // 中序遍历 void inOrder(TreeNode *root) { if (root == NULL) { return; } inOrder(root->left); printf("%c ", root->data); inOrder(root->right); } // 后序遍历 void postOrder(TreeNode *root) { if (root == NULL) { return; } postOrder(root->left); postOrder(root->right); printf("%c ", root->data); } // 层序遍历 void levelOrder(TreeNode *root) { if (root == NULL) { return; } TreeNode *queue[1000]; int front = 0, rear = 0; queue[rear++] = root; while (front != rear) { TreeNode *temp = queue[front++]; printf("%c ", temp->data); if (temp->left != NULL) { queue[rear++] = temp->left; } if (temp->right != NULL) { queue[rear++] = temp->right; } } } // 统计叶子节点数 int countLeaf(TreeNode *root) { if (root == NULL) { return 0; } if (root->left == NULL && root->right == NULL) { return 1; } return countLeaf(root->left) + countLeaf(root->right); } // 计算的深度 int depth(TreeNode *root) { if (root == NULL) { return 0; } int leftDepth = depth(root->left); int rightDepth = depth(root->right); return leftDepth > rightDepth ? leftDepth + 1 : rightDepth + 1; } int main() { printf("请输入二叉树,用^表示空:\n"); TreeNode *root = createTree(); printf("\n先序遍历结果="); preOrder(root); printf("\n中序遍历结果="); inOrder(root); printf("\n后序遍历结果="); postOrder(root); printf("\n层序遍历结果="); levelOrder(root); printf("\n叶子数=%d\n", countLeaf(root)); printf("的深度=%d\n", depth(root)); return 0; } ``` 注意事项: 1. 输入时请保证二叉树的形式正确,即左右子都用 '^' 表示为空。 2. 叶子节点数的计算方法为:左右子都为空时为 1,否则为左子叶子节点数加上右子叶子节点数之和。 3. 深度的计算方法为:左右子深度较大的值加上 1。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值