Lowest Common Ancestor of a Binary Tree
2016-06-07 19:50
232 查看
题目描述:
Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree.
According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined
between two nodes v and w as the lowest node in T that has both v and w as descendants (where we allow a node to be a descendant of itself).”
For example, the lowest common ancestor (LCA) of nodes
Another example is LCA of nodes
since a node can be a descendant of itself according to the LCA definition.
解题思路:分别找到从树的根结点root到结点p和q的路径,然后两条路径的最后一个公共结点即为所要求的结点p和q的最小公共祖先
AC代码如下:
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
class Solution {
public:
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
if (root == NULL || p == NULL || q == NULL) return NULL;
vector<TreeNode*> path1;
vector<TreeNode*> path2;
getPath(root, p, path1);
getPath(root, q, path2);
TreeNode* ans = lastCommonNode(path1, path2);
return ans;
}
bool getPath(TreeNode* root, TreeNode* p, vector<TreeNode*>& path){
if (root == p){
path.push_back(root);
return true;
}
bool findLeft = false;
bool findRight = false;
path.push_back(root);
if (root->left != NULL){
findLeft = getPath(root->left, p, path);
}
if (!findLeft && root->right != NULL){
findRight = getPath(root->right, p, path);
}
if (!findLeft && !findRight){
path.pop_back();
return false;
}
return true;
}
TreeNode* lastCommonNode(vector<TreeNode*>& path1, vector<TreeNode*>& path2){
vector<TreeNode*>::iterator it1 = path1.begin();
vector<TreeNode*>::iterator it2 = path2.begin();
TreeNode* ans = NULL;
while (it1 != path1.end() && it2 != path2.end()){
if (*it1 == *it2){
ans = *it1;
}
++it1;
++it2;
}
return ans;
}
};
Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree.
According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined
between two nodes v and w as the lowest node in T that has both v and w as descendants (where we allow a node to be a descendant of itself).”
_______3______ / \ ___5__ ___1__ / \ / \ 6 _2 0 8 / \ 7 4
For example, the lowest common ancestor (LCA) of nodes
5and
1is
3.
Another example is LCA of nodes
5and
4is
5,
since a node can be a descendant of itself according to the LCA definition.
解题思路:分别找到从树的根结点root到结点p和q的路径,然后两条路径的最后一个公共结点即为所要求的结点p和q的最小公共祖先
AC代码如下:
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
class Solution {
public:
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
if (root == NULL || p == NULL || q == NULL) return NULL;
vector<TreeNode*> path1;
vector<TreeNode*> path2;
getPath(root, p, path1);
getPath(root, q, path2);
TreeNode* ans = lastCommonNode(path1, path2);
return ans;
}
bool getPath(TreeNode* root, TreeNode* p, vector<TreeNode*>& path){
if (root == p){
path.push_back(root);
return true;
}
bool findLeft = false;
bool findRight = false;
path.push_back(root);
if (root->left != NULL){
findLeft = getPath(root->left, p, path);
}
if (!findLeft && root->right != NULL){
findRight = getPath(root->right, p, path);
}
if (!findLeft && !findRight){
path.pop_back();
return false;
}
return true;
}
TreeNode* lastCommonNode(vector<TreeNode*>& path1, vector<TreeNode*>& path2){
vector<TreeNode*>::iterator it1 = path1.begin();
vector<TreeNode*>::iterator it2 = path2.begin();
TreeNode* ans = NULL;
while (it1 != path1.end() && it2 != path2.end()){
if (*it1 == *it2){
ans = *it1;
}
++it1;
++it2;
}
return ans;
}
};
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- 关于指针的一些事情
- c++ primer 第五版 笔记前言
- share_ptr的几个注意点
- Lua中调用C++函数示例
- Lua教程(一):在C++中嵌入Lua脚本
- Lua教程(二):C++和Lua相互传递数据示例
- C++联合体转换成C#结构的实现方法
- C++高级程序员成长之路
- C++编写简单的打靶游戏
- C++ 自定义控件的移植问题
- C++变位词问题分析
- C/C++数据对齐详细解析
- C++基于栈实现铁轨问题
- C++中引用的使用总结
- 使用Lua来扩展C++程序的方法
- C++中调用Lua函数实例
- Lua和C++的通信流程代码实例
- C与C++之间相互调用实例方法讲解
- 解析C++中派生的概念以及派生类成员的访问属性