您的位置:首页 > 其它

寻找二叉树两个节点的最低公共祖先

2016-03-24 10:18 387 查看
给定一棵树,同时给出树中的两个结点(n1和n2),求它们的最低公共祖先。也就是常见的LCA(Lowest Common Ancestor )问题


方法一

下面是一个简单的复杂度为 O(n) 的算法,解决LCA问题

1) 找到从根到n1的路径,并存储在一个向量或数组中。

2)找到从根到n2的路径,并存储在一个向量或数组中。

3) 遍历这两条路径,直到遇到一个不同的节点,则前面的那个即为最低公共祖先.

下面的C++的程序实现

01
//
O(n) 解决 LCA
02
#include
<iostream>
03
#include
<vector>
04
using
namespace
std;
05
06
//二叉树节点
07
struct
Node
08
{
09
int
key;
10
struct
Node
*left, *right;
11
};
12
//公用函数,生成一个节点
13
Node
* newNode(
int
k)
14
{
15
Node
*temp =
new
Node;
16
temp->key
= k;
17
temp->left
= temp->right = NULL;
18
return
temp;
19
}
20
//找到从root到
节点值为key的路径,存储在path中。没有的话返回-1
21
bool
findpath(Node
* root,vector<
int
>
&path,
int
key){
22
if
(root
== NULL)
return
false
;
23
path.push_back(root->key);
24
if
(root->key
== key)
return
true
;
25
//左子树或右子树
是否找到,找到的话当前节点就在路径中了
26
bool
find
=  ( findpath(root->left, path, key) || findpath(root->right,path ,key) );
27
if
(find)
return
true
;
28
//该节点下未找到就弹出
29
path.pop_back();
30
return
false
;
31
}
32
33
int
findLCA(Node
* root,
int
key1,
int
key2){
34
vector<
int
>
path1,path2;
35
bool
find1
= findpath(root, path1, key1);
36
bool
find2
= findpath(root, path2, key2);
37
if
(find1
&& find2){
38
int
ans
;
39
for
(
int
i=0;
i<path1.size(); i++){
40
if
(path1[i]
!= path2[i]){
41
break
;
42
}
else
43
ans
= path1[i];
44
}
45
return
ans;
46
}
47
return
-1;
48
}
49
50
//
Driver program to test above functions
51
int
main()
52
{
53
//
按照上面的图来创创建树
54
Node
* root = newNode(1);
55
root->left
= newNode(2);
56
root->right
= newNode(3);
57
root->left->left
= newNode(4);
58
root->left->right
= newNode(5);
59
root->right->left
= newNode(6);
60
root->right->right
= newNode(7);
61
cout
<<
"LCA(4,
5) = "
<<
findLCA(root, 4, 5);
62
cout
<<
"\nLCA(4,
6) = "
<<
findLCA(root, 4, 6);
63
cout
<<
"\nLCA(3,
4) = "
<<
findLCA(root, 3, 4);
64
cout
<<
"\nLCA(2,
4) = "
<<
findLCA(root, 2, 4);
65
return
0;
66
}
输出:

1
LCA(4,
5) = 2
2
LCA(4,
6) = 1
3
LCA(3,
4) = 1
4
LCA(2,
4) = 2
时间复杂度: O(n), 树被遍历了两次,每次遍历复杂度不超过n,然后比较路径。


第二种方法(只遍历一次)

上面的方法虽然是O(n),但是操作依然繁琐了一点,并且需要额外的空间来存储路径。其实可以只遍历一次,利用递归的巧妙之处。学好二叉树,其实就是学好递归。

从root开始遍历,如果n1和n2中的任一个和root匹配,那么root就是LCA。 如果都不匹配,则分别递归左、右子树,如果有一个 key(n1或n2)出现在左子树,并且另一个key(n1或n2)出现在右子树,则root就是LCA. 如果两个key都出现在左子树,则说明LCA在左子树中,否则在右子树。

01
/*
只用一次遍历解决LCA */
02
#include
<iostream>
03
using
namespace
std;
04
struct
Node
05
{
06
struct
Node
*left, *right;
07
int
key;
08
};
09
Node*
newNode(
int
key)
10
{
11
Node
*temp =
new
Node;
12
temp->key
= key;
13
temp->left
= temp->right = NULL;
14
return
temp;
15
}
16
17
//
返回n1和n2的 LCA的指针
18
//
假设n1和n2都出现在树中
19
struct
Node
*findLCA(
struct
Node*
root,
int
n1,
int
n2)
20
{
21
if
(root
== NULL)
return
NULL;
22
23
//
只要n1 或 n2 的任一个匹配即可
24
//
(注意:如果 一个节点是另一个祖先,则返回的是祖先节点。因为递归是要返回到祖先的 )
25
if
(root->key
== n1 || root->key == n2)
26
return
root;
27
//
分别在左右子树查找
28
Node
*left_lca = findLCA(root->left, n1,n2);
29
Node
*right_lca = findLCA(root->right, n1,n2);
30
//
如果都返回非空指针 Non-NULL, 则说明两个节点分别出现了在两个子树中,则当前节点肯定为LCA
31
if
(left_lca
&& right_lca)
return
root;
32
//
如果一个为空,在说明LCA在另一个子树
33
return
(left_lca
!= NULL)? left_lca: right_lca;
34
}
35
36
//测试
37
int
main()
38
{
39
//
构造上面图中的树
40
Node
* root = newNode(1);
41
root->left
= newNode(2);
42
root->right
= newNode(3);
43
root->left->left
= newNode(4);
44
root->left->right
= newNode(5);
45
root->right->left
= newNode(6);
46
root->right->right
= newNode(7);
47
cout
<<
"LCA(4,
5) = "
<<
findLCA(root, 4, 5)->key;
48
cout
<<
"\nLCA(4,
6) = "
<<
findLCA(root, 4, 6)->key;
49
cout
<<
"\nLCA(3,
4) = "
<<
findLCA(root, 3, 4)->key;
50
cout
<<
"\nLCA(2,
4) = "
<<
findLCA(root, 2, 4)->key;
51
return
0;
52
}
时间复杂度为O(n),但是上面的方法还是有所局限的,必须保证两个要查找的节点n1和n2都出现在树中。如果n1不在树中,则会返回n2为LCA,理想答案应该为NULL。要解决这个问题,可以先查找下 n1和n2是否出现在树中,然后加几个判断即可。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: