An inorder binary tree traversal can be implemented in a non-recursive way with a stack. For example, suppose that when a 6-node binary tree (with the keys numbered from 1 to 6) is traversed, the stack operations are: push(1); push(2); push(3); pop(); pop(); push(4); pop(); pop(); push(5); push(6); pop(); pop(). Then a unique binary tree (shown in Figure 1) can be generated from this sequence of operations. Your task is to give the postorder traversal sequence of this tree.
Figure 1
Input Specification:
Each input file contains one test case. For each case, the first line contains a positive integer N (≤30) which is the total number of nodes in a tree (and hence the nodes are numbered from 1 to N). Then 2N lines follow, each describes a stack operation in the format: “Push X” where X is the index of the node being pushed onto the stack; or “Pop” meaning to pop one node from the stack.
Output Specification:
For each test case, print the postorder traversal sequence of the corresponding tree in one line. A solution is guaranteed to exist. All the numbers must be separated by exactly one space, and there must be no extra space at the end of the line.
Sample Input:
1 2 3 4 5 6 7 8 9 10 11 12 13
6 Push 1 Push 2 Push 3 Pop Pop Push 4 Pop Pop Push 5 Push 6 Pop Pop
Suppose that all the keys in a binary tree are distinct positive integers. A unique binary tree can be determined by a given pair of postorder and inorder traversal sequences. And it is a simple standard routine to print the numbers in level-order. However, if you think the problem is too simple, then you are too naive. This time you are supposed to print the numbers in “zigzagging order” – that is, starting from the root, print the numbers level-by-level, alternating between left to right and right to left. For example, for the following tree you must output: 1 11 5 8 17 12 20 15.
Input Specification:
Each input file contains one test case. For each case, the first line gives a positive integer N (≤30), the total number of nodes in the binary tree. The second line gives the inorder sequence and the third line gives the postorder sequence. All the numbers in a line are separated by a space.
Output Specification:
For each test case, print the zigzagging sequence of the tree in a line. All the numbers in a line must be separated by exactly one space, and there must be no extra space at the end of the line.
Given a non-empty tree with root R, and with weight $W_i$ assigned to each tree node $T_i$. The weight of a path from $R$ to $L$ is defined to be the sum of the weights of all the nodes along the path from R to any leaf node L.
Now given any weighted tree, you are supposed to find all the paths with their weights equal to a given number. For example, let’s consider the tree showed in the following figure: for each node, the upper number is the node ID which is a two-digit number, and the lower number is the weight of that node. Suppose that the given number is 24, then there exists 4 different paths which have the same given weight: {10 5 2 7}, {10 4 10}, {10 3 3 6 2} and {10 3 3 6 2}, which correspond to the red edges in the figure.
Input Specification:
Each input file contains one test case. Each case starts with a line containing 0<N≤100, the number of nodes in a tree, M (<N), the number of non-leaf nodes, and 0<S<230, the given weight number. The next line contains N positive numbers where W**i (<1000) corresponds to the tree node T**i. Then M lines follow, each in the format:
1
ID K ID[1] ID[2] ... ID[K]
where ID is a two-digit number representing a given non-leaf node, K is the number of its children, followed by a sequence of two-digit ID‘s of its children. For the sake of simplicity, let us fix the root ID to be 00.
Output Specification:
For each test case, print all the paths with weight S in non-increasing order. Each path occupies a line with printed weights from the root to the leaf in order. All the numbers must be separated by a space with no extra space at the end of the line.
Note: sequence {A1,A2,⋯,A**n} is said to be greater than sequence {B1,B2,⋯,B**m} if there exists 1≤k<*min*{*n*,*m*} such that *Ai=B**i for i=1,⋯,k, and A**k+1>*B**k+1.
7 A 1 B 1 C 1 D 3 E 3 F 6 G 6 4 A 00000 B 00001 C 0001 D 001 E 01 F 10 G 11 A 01010 B 01011 C 0100 D 011 E 10 F 11 G 00 A 000 B 001 C 010 D 011 E 100 F 101 G 110 A 00000 B 00001 C 0001 D 001 E 00 F 10 G 11
#define R register #define L long #define LL long long #define I inline #define U unsigned usingnamespace std;
I LL read() { R LL x; R bool f; R char c; for(f=0;(c=getchar())<'0'||c>'9';f=c=='-'); for(x=c-'0';(c=getchar())>='0'&&c<='9';x=(x<<3)+(x<<1)+c-'0'); return f?-x:x; }
How would you implement mergesort without using recursion?
The idea of iterative mergesort is to start from N sorted sublists of length 1, and each time to merge a pair of adjacent sublists until one sorted list is obtained. You are supposed to implement the key function of merging.
Format of functions:
1
voidmerge_pass( ElementType list[], ElementType sorted[], int N, int length );
The function merge_pass performs one pass of the merge sort that merges adjacent pairs of sublists from list into sorted. N is the number of elements in the list and length is the length of the sublists.
voidmerge_pass( ElementType list[], ElementType sorted[], int N, int length );
voidoutput( ElementType list[], int N ) { int i; for (i=0; i<N; i++) printf("%d ", list[i]); printf("\n"); }
voidmerge_sort( ElementType list[], int N ) { ElementType extra[MAXN]; /* the extra space required */ int length = 1; /* current length of sublist being merged */ while( length < N ) { merge_pass( list, extra, N, length ); /* merge list into extra */ output( extra, N ); length *= 2; merge_pass( extra, list, N, length ); /* merge extra back to list */ output( list, N ); length *= 2; } }
typedefstructTNode *BinTree; structTNode{ int Key; BinTree Left; BinTree Right; };
BinTree BuildTree(); /* details omitted */ BinTree KthLargest( BinTree T, int K );
intmain() { BinTree T, P; int K;
T = BuildTree(); scanf("%d", &K); P = KthLargest(T, K); printf("%d\n", P->Key); if (P->Left) printf("%d\n", P->Left->Key); elseprintf("NULL\n"); if (P->Right) printf("%d\n", P->Right->Key); elseprintf("NULL\n");
return0; } /* Your function will be put here */
Sample Input: (for the following tree)
1
4
Sample Output:
1 2 3
5 NULL 7
Code Size Limit
16 KB
Time Limit
400 ms
Memory Limit
64 MB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
//本质二叉树,RDL int cnt=0; BinTree KthLargest( BinTree T, int K ){ BinTree P,Q; if(T){ P=KthLargest(T->Right,K); if(P) return P;//? 在递归层中返回了T',要传回 cnt++; if(cnt==K){ return T; } Q=KthLargest(T->Left,K); if(Q) return Q; } elsereturnNULL; }
2-3(funx) Rank a Linked List (II)
A linked list of n nodes is stored in an array of n elements. Each element contains an integer data and a next pointer which is the array index of the next element. It is guaranteed that the given list is linear – that is, every node, except the first one, has a unique previous node; and every node, except the last one, has a unique next node.
You are supposed to write a function to number these nodes in reverse order, starting from the last node, by numbers from 1 to n. These numbers are called the ranks of the nodes.
Format of function:
1
voidRanking( List A[], int n );
where List is defined as the following:
1 2 3 4 5
typedefstructNode { int data; int next; int rank; } List;
typedefstructNode { int data; int next; int rank; } List;
voidRanking( List A[], int n );
intmain() { int n, i; List *A;
scanf("%d", &n); A = (List *)malloc(sizeof(List)*n); for (i=0; i<n; i++) scanf("%d", &A[i].next); Ranking(A, n); for (i=0; i<n; i++) printf("%d ", A[i].rank); printf("\n");
return0; }
/* Your function will be put here */
Sample Input:
1 2
5 3 -1 0 1 2
Sample Output:
1
3 1 4 2 5
Hint:
The given linked list is stored as 4->2->0->3->1->NULL. Hence the 0th element is ranked 3 since it is the 3rd node counted from the last one in the list; the 1st element is ranked 1 since it is the last node in the list; and so on so forth.
int vis[100007]; voidRanking( List A[], int n ){ int head; for(int i=0;i<n;i++){ if(A[i].rank!=-1) vis[A[i].next]=1; } for(int i=0;i<n;i++){ if(!vis[i]) { head=i; break; } } int cnt=n; A[head].rank=cnt--; int temp=A[head].next; while(temp!=-1){ A[temp].rank=cnt--; temp=A[temp].next; } }
The word "this" is the word with the highest frequency.
Longlonglonglongword should be cut off, so is considered as the same as longlonglonglonee. But this_8 is different than this, and this, and this...# this line should be ignored.
typedefenum {false, true} bool; #define MaxVertexNum 10 /* maximum number of vertices */ typedefint Vertex; /* vertices are numbered from 1 to MaxVertexNum */
Given the relations of all the activities of a project, you are supposed to find the earliest completion time of the project.
Input Specification:
Each input file contains one test case. Each case starts with a line containing two positive integers N (≤100), the number of activity check points (hence it is assumed that the check points are numbered from 0 to N−1), and M, the number of activities. Then M lines follow, each gives the description of an activity. For the i-th activity, three non-negative numbers are given: S[i], E[i], and L[i], where S[i] is the index of the starting check point, E[i] of the ending check point, and L[i] the lasting time of the activity. The numbers in a line are separated by a space.
Output Specification:
For each test case, if the scheduling is possible, print in a line its earliest completion time; or simply output “Impossible”.
In Professor McGonagall’s class of Transfiguration, Harry Potter is learning how to transform one object into another by some spells. He has learnt that, to turn a cat into a mouse one can say docamo! To reverse the effect, simply say decamo! Formally speaking, the transfiguration spell to transform between object A and object B is said to be S if there are two spells, doS and deS, to turn A into B and vice versa, respectively.
In some cases, short-cut spells are defined to make transfiguration easier. For example, suppose that the spell to transform a cat to a mouse is docamo, and that to transform a mouse into a fatmouse is dofamo, then to turn a cat into a fatmouse one may say docamodofamo! Or if a shot-cut spell is defined to be cafam, one may get the same effect by saying docafam!
Time is passing by quickly and the Final Exam is coming. By the end of the transfiguration exam, students will be requested to show Professor McGonagall several objects transformed from the initial objects they bring to the classroom. Each of them is allowed to bring 1 object only.
Now Harry is coming to you for help: he needs a program to select the object he must take to the exam, so that the maximum length of any spell he has to say will be minimized. For example, if cat, mouse, and fatmouse are the only three objects involved in the exam, then mouse is the one that Harry should take, since it will take a 6-letter spell to turn a mouse into either a cat or a fatmouse. Cat is not a good choice since it will take at least a 7-letter spell to turn it into a fatmouse. And for the same reason Harry must not take a fatmouse.
Input Specification:
Each input file contains one test case. For each case, the first line contains two positive integers N (≤100) and M, which are the total number of objects involved in the exam and the number of spells to be tested, respectively. For the sake of simplicity, the objects are numbered from 1 to N. Then M lines follow, each contains 3 integers, separated by a space: the numbers of two objects, and the length of the spell to transform between them.
Output Specification:
For each test case, print in one line the number of the object which Harry must take to the exam, and the maximum length of the spell he may have to say. The numbers must be separated by a space.
If it is impossible to complete all the transfigurations by taking one object only, simply output 0. If the solution is not unique, output the one with the smallest number.