1143 Lowest Common Ancestor
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:
Given any two nodes in a BST, you are supposed to find their LCA.
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.
For each given pair of U and V, print in a line LCA of U and V is A.
if the LCA is found and A
is the key. But if A
is one of U and V, print X is an ancestor of Y.
where X
is A
and Y
is the other node. If U or V is not found in the BST, print in a line ERROR: U is not found.
or ERROR: V is not found.
or ERROR: U and V are not found.
.
- 6 8
- 6 3 1 2 5 4 8 7
- 2 5
- 8 7
- 1 9
- 12 -3
- 0 8
- 99 99
- 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.
总结:这是道题目其实挺简单的,主要考察的是对耗时的控制,所以点在于控制好时间复杂度
唉,我只会常规的方法(先建立一颗二叉搜索数,然后遍历二叉搜索树,依据一定的方法找到对一个的位置即可)
代码:
- #include
- #include
- using namespace std;
-
- struct node{
- int val;
- struct node *left,*right;
- };
- node *build(node *root,int v){
- if(root==NULL){
- root=new node();
- root->val=v;
- root->left=root->right=NULL;
- }
- else if(v<=root->val) root->left=build(root->left,v);
- else root->right=build(root->right,v);
- return root;
- }
- void dfs(node *root,int a,int b){
- if((a>root->val && b
val) || (aval && b>root->val)){ - printf("LCA of %d and %d is %d.\n",a,b,root->val);
- return;
- }
- else if(a==root->val){
- printf("%d is an ancestor of %d.\n",a,b);
- return;
- }
- else if(b==root->val){
- printf("%d is an ancestor of %d.\n",b,a);
- return;
- }
- int t=max(a,b);
- if(root->val>t) dfs(root->left,a,b);
- if(root->val
dfs(root->right,a,b); - }
- int main(){
- int m,n;
- scanf("%d%d",&m,&n);
- int a[n];
- unordered_map<int,int> p;
-
- node *root=NULL;
- for(int i=0;i
- scanf("%d",&a[i]);
- p[a[i]]=1;
- root=build(root,a[i]);
- }
- for(int i=0;i
- int a,b;
- scanf("%d%d",&a,&b);
- if(!p[a] && !p[b]) printf("ERROR: %d and %d are not found.\n",a,b);
- else if(!p[a]) printf("ERROR: %d is not found.\n",a);
- else if(!p[b]) printf("ERROR: %d is not found.\n",b);
- else dfs(root,a,b);
- }
-
- return 0;
- }
柳神代码:
简洁明了,想法太妙了!
- #include
- #include
- using namespace std;
-
- int main(){
- int m,n;
- scanf("%d%d",&m,&n);
- int w[n];
-
- unordered_map<int,bool> p;
- for(int i=0;i
- scanf("%d",&w[i]);
- p[w[i]]=true;
- }
- for(int i=0;i
- int a,b,t;
- scanf("%d%d",&a,&b);
- for(int j=0;j
- t=w[j];
- if((t>a && tb && tbreak;
- }
- if(!p[a] && !p[b]) printf("ERROR: %d and %d are not found.\n",a,b);
- else if(!p[a] || !p[b]) printf("ERROR: %d is not found.\n",!p[a]?a:b);
- else if(t==a || t==b) printf("%d is an ancestor of %d.\n",t==a?a:b,t==a?b:a);
- else printf("LCA of %d and %d is %d.\n",a,b,t);
- }
-
- return 0;
- }
2022.10.31 第一遍已经算是刷完了(接下来好好准备第二遍,明年3月份的考试!)
好好学习,天天向上!
我要考研!
-
相关阅读:
【Qt系列】QtableWidget表格列宽自适应表格大小
QtCreator5.15.0编译全过程记录
“奶爸车”成功了,那“女性车”呢?
MES系统是怎样实现生产调度的?
3. 内核解压-确定解压信息
迅为瑞芯微RK3399开发板设置Buildroot文件系统测试MYSQL允许远程访问
Python3入门教程||Python3 XML解析
ubuntu http 服务器响应
git克隆一直报错remote: HTTP Basic: Access denied
彻底理解并解决服务器出现大量TIME_WAIT - 第二篇
-
原文地址:https://blog.csdn.net/weixin_50679551/article/details/127624025