1个回答

C语言求二叉树结点个数？

C语言构造二叉树的问题，二叉树从键盘的输入

Description Little Valentine liked playing with binary trees very much. Her favorite game was constructing randomly looking binary trees with capital letters in the nodes. This is an example of one of her creations: D / \ / \ B E / \ \ / \ \ A C G / / F To record her trees for future generations, she wrote down two strings for each tree: a preorder traversal (root, left subtree, right subtree) and an inorder traversal (left subtree, root, right subtree). For the tree drawn above the preorder traversal is DBACEGF and the inorder traversal is ABCDEFG. She thought that such a pair of strings would give enough information to reconstruct the tree later (but she never tried it). Now, years later, looking again at the strings, she realized that reconstructing the trees was indeed possible, but only because she never had used the same letter twice in the same tree. However, doing the reconstruction by hand, soon turned out to be tedious. So now she asks you to write a program that does the job for her! Input The input will contain one or more test cases. Each test case consists of one line containing two strings preord and inord, representing the preorder traversal and inorder traversal of a binary tree. Both strings consist of unique capital letters. (Thus they are not longer than 26 characters.) Input is terminated by end of file. Output For each test case, recover Valentine's binary tree and print one line containing the tree's postorder traversal (left subtree, right subtree, root). Sample Input DBACEGF ABCDEFG BCAD CBAD Sample Output ACBFGED CDAB

Problem Description Today CodeFamer is going to cut trees.There are N trees standing in a line. They are numbered from 1 to N. The tree numbered i has height hi. We say that two uncutted trees whose numbers are x and y are in the same block if and only if they are fitting in one of blow rules: 1)x+1=y or y+1=x; 2)there exists an uncutted tree which is numbered z, and x is in the same block with z, while y is also in the same block with z. Now CodeFamer want to cut some trees whose height is not larger than some value, after those trees are cut, how many tree blocks are there? Input Multi test cases (about 15). For each case, first line contains two integers N and&#160;Q separated by exactly one space, N indicates there are N trees, Q indicates there are Q queries. In the following N lines, there will appear h[1],h[2],h[3],…,h[N] which indicates the height of the trees. In the following Q lines, there will appear q[1],q[2],q[3],…,q[Q] which indicates CodeFamer’s queries. Please process to the end of file. [Technical Specification] 1≤N,Q≤50000 0≤h[i]≤1000000000(109) 0≤q[i]≤1000000000(109) Output For each q[i], output the number of tree block after CodeFamer cut the trees whose height are not larger than q[i]. Sample Input 3 2 5 2 3 6 2 Sample Output 0 2

C语言二叉树的节点查找问题（递归方法）

c语言建立二叉树怎么输入多组数据

#include <stdio.h> #include <stdlib.h> typedef struct BiTNode { char data; struct BiTNode *lchild,*rchild; } BiTNode,*BiTree;//定义结点类型 BiTree CreateBiTree()//创建树 { char p;BiTree T; scanf("%c",&p); if(p == '\$') T=NULL; else { T=(BiTNode *)malloc(sizeof(BiTNode));//为结点开辟空间 T->data=p; T->lchild=CreateBiTree(); T->rchild=CreateBiTree(); } return (T); } void PostOrder(BiTree T)//后序 { if(T!=NULL) { PostOrder(T->lchild); PostOrder(T->rchild); printf("%c",T->data); } } void main()//主函数 { BiTree Ta; Ta=CreateBiTree(); PostOrder(Ta); }

c语言实现二叉树，碰到些问题，希望大神可以指导我解决，到程序运行完成，谢谢！

_**#define STACK_INIT_SIZE 10 #define INCREMENT_SIZE 10 #include #include #include #include #include #define TRUE 1 #define FALSE 0 #define OK 1 #define ERROR -1 #define OVERFLOW -2 typedef int status; typedef char telemtype; typedef struct bitnode { telemtype data; struct bitnode *lchild, *rchild; } bitnode ,*bitree; status createbitree(bitree *t) { char ch; scanf("%c",&ch); if(ch='#')t=NULL; else { if(!(t=(bitree *)malloc(sizeof(bitree))))exit(OVERFLOW); (*t)->data=ch; creatbitree((*t)->lchild); creatbitree((*t)->rchild); } return OK; } status printelement(telemtype e) { printf("%c",e); return OK; } status preordertraverse(bitree t,status(* visit)(telemtype e)) { if (t) { if(visit(t->data)) if(preordertraverse(t->lchild,visit)) if(preordertraverse(t->rchild,visit)) return OK; return ERROR; } else return OK; } status zhongxu(bitree t,status(* visit)(telemtype e)) { if (t) { if(zhongxu(t->lchild,visit)) if(visit(t->data)) if(zhongxu(t->rchild,visit)) return OK; return ERROR; } else return OK; } int main(int argc, char *argv[]) { int bitnode; bitree t; t=NULL; createbitree(&t); printf("\n"); preordertraverse(&t,printelement); zhongxu(&t,printelement); return 0; } 错误是 E:\数据结构\main.c In function 'main': 77 19 E:\数据结构\main.c [Warning] passing argument 1 of 'preordertraverse' from incompatible pointer type 45 8 E:\数据结构\main.c [Note] expected 'bitree' but argument is of type 'struct bitnode **' 78 10 E:\数据结构\main.c [Warning] passing argument 1 of 'zhongxu' from incompatible pointer type 58 8 E:\数据结构\main.c [Note] expected 'bitree' but argument is of type 'struct bitnode **' 能帮忙修改下让它运行正常吗、？ 谢谢 真的很急 。__一天都在线的

C语言二叉树遍历的问题

``` #include<stdio.h> #include<stdlib.h> typedef struct node { int num; struct node *l; struct node *r; }LN; void Creat(LN *L); void Change(LN *L); int Print(int n); void main() { LN *L; int n; ii: printf("Press 0 to end: \n"); printf("Press 1 to Creat a Linklist: \n"); printf("Press 2 to Change the child: \n"); scanf("%d",&n); switch(n) { case 0: system("CLS"); goto cc; case 1: system("CLS"); printf("Input the root number: \n"); Creat(L); system("CLS"); break; case 2: Change(L); break; } goto ii; cc:; } void Creat(LN *L) { int n; scanf("%d",&n); if(n==0) { L=NULL; } else { L=(LN*)malloc(sizeof(LN)); L->num=n; printf("Creat %d's leftchlid: \n",n); Creat(L->l); printf("Creat %d's rightchild: \n",n); Creat(L->r); } } void Change(LN *L) { if (L!=NULL) { printf("%d",L->num); Change(L->l); Change(L->r); } } ``` 我的Change函数是先序遍历，小弟刚学实在不知道错在哪里。怎么从先序遍历算法中交换左右孩子，谢谢大神门！！！！！ 还有怎么按层次遍历，感觉按层次没有办法用递归算法 如果我要想从大到小的顺序输出二叉排序树的各结点的算法，怎么实现，跪求大神门的指教，感谢！

C语言算法实现查找二叉树最短路径的问题

Problem Description Everyone konws how to traveling a tree using the DFS strategy. But we also know that there are many ways to do so. For example, giving a tree as the following picture, we may get three ways: 010011, 001101, 01010011. 0 stands for the down operation while 1 means the up operation. Now we make a constraint: if one node has k direct childs, you can visit a node at most 2*k times, if k == 0, you can visit it only once, in the example, the root has two direct child. Like the example, you can only get two ways: 010011, 001101. Because the way 01010011 will visit the node in yellow four times. Here is the problem: ACboy drawed a tree, but is not very nice, so he won't show you the picture. Instread he will give you two strings indicating that the ways to travel the tree. Of cource, the strings will only contain 0 and 1. And your mission is to tell whether ACboy is telling the truth. For example, he drawed a picture as the following, if he give you 010011 and 001101, then he is telling the truth, but if he give you 010011 and 01010011, you konw that he is telling a lie. Input On the first line of input is a single positive integer n, telling the number of test scenarios to follow.Each test case consists of two lines, each containing a string of the characters '0' and '1' of length at most 3000, both describing a way to travel the tree. Output For each test case output a line containing the word "True" or the word "False", depending on whether ACboy is telling the truth. Sample Input 2 010011 001101 010011 01010011 Sample Output True False

Problem Description As we know, Rikka is poor at math. Yuta is worrying about this situation. He's given Rikka many math tasks to practice but she hasn't solved any of them. So, today he comes up with a simple problem to help her build up confidence: Here is a tree with m nodes, you can delete some of the nodes on the tree and there mustn't be any edges connecting two remained nodes. You need to maximize the number of the points remained. Rikka thinks this task is too simple, so she comes up with a new problem: At first there is a tree with only one node. And then each time she links a new node to the tree. After each operation, you need to tell her the maximum number of the points remained (as described above). This problem is too difficult for Rikka to solve. Can you help her? Input There are no more than 100 testcases and there are no more than 3 testcases with n>103. For each testcase, the first line contains a number n (1≤n≤105). Then n−1 lines follow. The ith line contains a single number fi (0≤fi<i), which means that after the ith operation there is a new node numbered i and there is an edge between node i and node fi. Output For each operation you need to print a single line with a single number - the answer after this operation. Sample Input 4 0 0 1 Sample Output 1 2 2

C语言求大神帮忙看看写的二叉树删除，为什么我的这道程序执行顺序异常

c语言实现英文文本词频统计

c语言结课课程设计，希望有大佬可以指点一下小白 单词词频统计 设计内容： 写程序处理英语文本文件，统计文件中所出现的各单词在文本中出现的频次。 设计功能： 1、 将英文段落以文本文件，并在屏幕上显示。 2、 处理读入的文本文件，提取段落中的英文单词，并且统计各单词出现的次数。 3、 将各单词及其出现频次打印出来，并且将频次为前十的单词的频率用柱状图表示出来。 如果VC6.0环境不能画柱状图的话，就只需要把所有单词的出现频次打出来，再把前十的单词和频率输出就好。 求指电 跪求大佬帮助

Problem Description Teacher Mai has m+1 trees, T0,T1,⋯,Tm. T0 consists one vertex numbered 0. He generated the Ti in this way. Get a copy of Tai and Tbi. Add an edge with length li between vertex numbered ci in T′ai and di in T′bi. Relabel the vertices in the new tree. Let k be the number of vertices in T′ai. He keeps labels of vertices in T′ai the same, and adds k to labels of vertices in T′bi. If there is a tree T with n vertices v0,v1,v2,⋯,vn−1, F(T)=∑n−1i=0∑n−1j=i+1d(vi,vj)(d(vi,vj) means the distance between the vi and vj). For every i(1≤i≤m), he wants to know F(Ti). Input There are multiple test cases(about 100). For each test case, the first line contains one number m(1≤m≤60), the following are m lines. The i-th lines contains five numbers ai,bi,ci,di,li(0≤ai,bi<i,0≤li≤109). It's guarenteed that there exists a vertex numbered ci in Tai and there exists a vertex numbered di in Tbi. Output For each test case, print F(Ti) modulo 109+7 in the i-th line. Sample Input 3 0 0 0 0 2 1 1 0 0 4 2 2 1 0 3 Sample Output 2 28 216

Problem Description There are some queries on a tree which has n nodes. Every query is described as two integers (X, Y).For each query, you should find the maximum weight of the edges in set E, which satisfies the following two conditions. 1) The edge must on the path from node X to node 1. 2) The edge’s weight should be lower or equal to Y. Now give you the tree and queries. Can you find out the answer for each query? Input The first line of the input is an integer T, indicating the number of test cases. For each case, the first line contains an integer n indicates the number of nodes in the tree. Then n-1 lines follows, each line contains three integers X, Y, W indicate an edge between node X and node Y whose value is W. Then one line has one integer Q indicates the number of queries. In the next Q lines, each line contains two integers X and Y as said above. Output For each test case, you should output Q lines. If no edge satisfy the conditions described above，just output “-1” for this query. Otherwise output the answer for this query. Sample Input 1 3 1 2 7 2 3 5 4 3 10 3 7 3 6 3 4 Sample Output 7 7 5 -1

loonggg读完需要3分钟速读仅需 1 分钟大家好，我是你们的校长。我之前讲过，这年头，只要肯动脑，肯行动，程序员凭借自己的技术，赚钱的方式还是有很多种的。仅仅靠在公司出卖自己的劳动时...

MySQL数据库面试题（2020最新版）

!大部分程序员只会写3年代码

HTTP与HTTPS的区别