The lowest common ancestor (LCA) of two nodes U and V in a tree is the deepest node that has both U and V as descendants.
Given any two nodes in a binary tree, you are supposed to find their LCA.
Input Specification:
Each input file contains one test case. For each case, the first line gives two positive integers: M (≤ 1,000), the number of pairs of nodes to be tested; and N (≤ 10,000), the number of keys in the binary tree, respectively. In each of the following two lines, N distinct integers are given as the inorder and preorder traversal sequences of the binary tree, respectively. It is guaranteed that the binary tree can be uniquely determined by the input sequences. Then M lines follow, each contains a pair of integer keys U and V. All the keys are in the range of int.
Output Specification:
For each given pair of U and V, print in a line
LCA of U and V is A.
if the LCA is found andA
is the key. But ifA
is one of U and V, printX is an ancestor of Y.
whereX
isA
andY
is the other node. If U or V is not found in the binary tree, print in a lineERROR: U is not found.
orERROR: V is not found.
orERROR: U and V are not found.
.Sample Input:
6 8 7 2 3 4 6 5 1 8 5 3 7 2 6 4 8 1 2 6 8 1 7 9 12 -3 0 8 99 99
Sample Output
题意:n个点中序和先序,m次询问,问a和b的最近的祖先节点是?
三种情况:
- a&&b不存在
- a||b不存在
- 存在:
- a、b都在左子树
- a、b都在右子树
- a、b一左一右,此时inroot即为最近共同祖先
- a是b的祖先 ain==inroot
- b是a的祖先 bin==inroot
#include<bits/stdc++.h>
using namespace std;
int in[10005],pre[10005];
map<int,int> pos;
void lca(int inl,int inr,int preroot,int a,int b){
if(inl>inr) return;
int inroot=pos[pre[preroot]];
if(pos[a]<inroot&&pos[b]<inroot) lca(inl,inroot-1,preroot+1,a,b);
else if(pos[a]>inroot&&pos[b]>inroot) lca(inroot+1,inr,preroot+1+inroot-inl,a,b);
else if(pos[a]>inroot&&pos[b]<inroot||pos[a]<inroot&&pos[b]>inroot) printf("LCA of %d and %d is %d.\n",a,b,in[inroot]);
else if(pos[a]==inroot) printf("%d is an ancestor of %d.\n",a,b);
else if(pos[b]==inroot) printf("%d is an ancestor of %d.\n",b,a);
}
int main(){
int m,n,a,b;
scanf("%d%d",&m,&n);
for(int i=1;i<=n;i++){
scanf("%d",&in[i]);
pos[in[i]]=i;
}
for(int i=1;i<=n;i++)
scanf("%d",&pre[i]);
for(int i=0;i<m;i++){
scanf("%d %d",&a,&b);
if(pos[a]==0&&pos[b]==0) printf("ERROR: %d and %d are not found.\n",a,b);
else if(pos[a]==0||pos[b]==0)printf("ERROR: %d is not found.\n",pos[a]==0?a:b);
else lca(1,n,1,a,b);
}
return 0;
}
In computer science, a heap is a specialized tree-based data structure that satisfies the heap property: if P is a parent node of C, then the key (the value) of P is either greater than or equal to (in a max heap) or less than or equal to (in a min heap) the key of C. A common implementation of a heap is the binary heap, in which the tree is a complete binary tree. (Quoted from Wikipedia at https://en.wikipedia.org/wiki/Heap_(data_structure))
Your job is to tell if a given complete binary tree is a heap.
Input Specification:
Each input file contains one test case. For each case, the first line gives two positive integers: M (≤ 100), the number of trees to be tested; and N (1 < N ≤ 1,000), the number of keys in each tree, respectively. Then M lines follow, each contains N distinct integer keys (all in the range of int), which gives the level order traversal sequence of a complete binary tree.
Output Specification:
For each given tree, print in a line
Max Heap
if it is a max heap, orMin Heap
for a min heap, orNot Heap
if it is not a heap at all. Then in the next line print the tree's postorder traversal sequence. All the numbers are separated by a space, and there must no extra space at the beginning or the end of the line.Sample Input:
3 8 98 72 86 60 65 12 23 50 8 38 25 58 52 82 70 60 10 28 15 12 34 9 8 56
Sample Output:
Max Heap 50 60 65 72 12 23 86 98 Min Heap 60 58 52 38 82 70 25 8 Not Heap 56 12 34 28 9 8 15 10
题意:给一个树的层序遍历,判断它是不是堆,是大顶堆还是小顶堆。输出这个树的后序遍历
首先根据a[1]和a[2]的大小比较判断可能是大顶还是小顶,分别赋值flag为1和-1,先根据层序遍历,从1~n/2【所有有孩子的结点】判断他们的孩子是不是满足flag的要求,如果有一个结点不满足,那就将flag=0表示这不是一个堆。根据flag输出是否是堆,大顶堆还是小顶堆,然后后序遍历,根据index分别遍历index*2和index*2,即他们的左右孩子,遍历完左右子树后输出根结点,即完成了后序遍历~
#include<bits/stdc++.h>
using namespace std;
int a[1005];
int n,m;
void postorder(int id) {
if(id>n) return;
postorder(id*2);
postorder(id*2+1);
printf("%d%s",a[id],id==1?"\n":" ");
}
int main() {
int flag;
scanf("%d%d",&m,&n);
while(m--) {
for(int i=1; i<=n; i++)
scanf("%d",&a[i]);
flag=a[1]>a[2]?1:-1;
for(int i=1; i<=n/2; i++) {
if(flag==1&&(a[i]<a[i*2]||(i*2+1<=n&&a[i]<a[i*2+1]))) flag=0;
if(flag==-1&&(a[i]>a[i*2]||(i*2+1<=n&&a[i]>a[2*i+1]))) flag=0;
}
if(flag==0) cout<<"Not Heap\n";
else printf("%s\n",flag==-1?"Min Heap":"Max Heap");
postorder(1);
}
return 0;
}
The lowest common ancestor (LCA) of two nodes U and V in a tree is the deepest node that has both U and V as descendants.
A binary search tree (BST) is recursively defined as a binary tree which has the following properties:
- The left subtree of a node contains only nodes with keys less than the node's key.
- The right subtree of a node contains only nodes with keys greater than or equal to the node's key.
- Both the left and right subtrees must also be binary search trees.
Given any two nodes in a BST, you are supposed to find their LCA.
Input Specification:
Each input file contains one test case. For each case, the first line gives two positive integers: M (≤ 1,000), the number of pairs of nodes to be tested; and N (≤ 10,000), the number of keys in the BST, respectively. In the second line, N distinct integers are given as the preorder traversal sequence of the BST. Then M lines follow, each contains a pair of integer keys U and V. All the keys are in the range of int.
Output Specification:
For each given pair of U and V, print in a line
LCA of U and V is A.
if the LCA is found andA
is the key. But ifA
is one of U and V, printX is an ancestor of Y.
whereX
isA
andY
is the other node. If U or V is not found in the BST, print in a lineERROR: U is not found.
orERROR: V is not found.
orERROR: U and V are not found.
.Sample Input:
6 8 6 3 1 2 5 4 8 7 2 5 8 7 1 9 12 -3 0 8 99 99
Sample Output:
LCA of 2 and 5 is 3. 8 is an ancestor of 7. ERROR: 9 is not found. ERROR: 12 and -3 are not found. ERROR: 0 is not found. ERROR: 99 and 99 are not found.
题意:给出一棵二叉搜索树的前序遍历,问结点a和b的共同最低祖先是谁。
分析:map<int, int> pos用来标记树中所有出现过的结点,遍历一遍pre数组,将当前结点标记为now,如果a和b分别在now的左、右,或者a、b其中一个就是当前now,即(a>=now&&b<=now||a<=now&&b>=now),说明找到了这个共同最低祖先now,退出当前循环,最后根据要求输出结果即可。
#include<bits/stdc++.h>
using namespace std;
map<int,int> pos;
int pre[10005];
int main() {
int n,m,a,b,now;
scanf("%d%d",&m,&n);
for(int i=1; i<=n; i++){
scanf("%d",&pre[i]);
pos[pre[i]]=i;
}
for(int i=0; i<m; i++) {
scanf("%d%d",&a,&b);
for(int j=1;j<=n;j++){
now=pre[j];
if(a>=now&&b<=now||a<=now&&b>=now) break;
}
if(pos[a]==0&&pos[b]==0) printf("ERROR: %d and %d are not found.\n",a,b);
else if(pos[a]==0||pos[b]==0) printf("ERROR: %d is not found.\n",pos[a]==0?a:b);
else if(now==a||now==b) printf("%d is an ancestor of %d.\n",now,now==a?b:a);
else printf("LCA of %d and %d is %d.\n", a, b, now);
}
return 0;
}