leetcode@ [236] Lowest Common Ancestor of a Binary Tree(Tree)
https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/
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 5 and 1 is 3. Another example is LCA of nodes 5 and 4 is 5, since a node can be a descendant of itself according to the LCA definition.
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
void findPath(TreeNode* root, vector<TreeNode* >& load, vector<TreeNode* >& path, TreeNode* p) {
if(root == NULL) return;
if(root == p) {
path = load;
return;
} load.push_back(root->left);
findPath(root->left, load, path, p);
load.pop_back(); load.push_back(root->right);
findPath(root->right, load, path, p);
load.pop_back();
}
void reverse_vector(vector<TreeNode* > v) {
vector<TreeNode* > res; res.clear(); for(int i=v.size()-;i>=;--i) res.push_back(v[i]);
v = res;
}
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
if(p == q) return p; vector<TreeNode* > load1, load2, path1, path2; TreeNode *r = root;
load1.push_back(r);
load2.push_back(r); findPath(r, load1, path1, p);
findPath(r, load2, path2, q); reverse_vector(path1);
reverse_vector(path2); int i = , j = , m = path1.size(), n = path2.size(), resi;
cout << m << n << endl;
while(i < m && j < n && path1[i] == path2[j]) {
resi = i; ++i; ++j;
} return path1[resi];
}
};
leetcode: 236
leetcode@ [236] Lowest Common Ancestor of a Binary Tree(Tree)的更多相关文章
- LeetCode (236):Lowest Common Ancestor of a Binary Search Tree
Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BS ...
- [LeetCode] 236. Lowest Common Ancestor of a Binary Tree 二叉树的最近公共祖先
Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. According ...
- [LeetCode] 235. Lowest Common Ancestor of a Binary Search Tree 二叉搜索树的最近公共祖先
Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BS ...
- [LeetCode] 236. Lowest Common Ancestor of a Binary Tree 二叉树的最小共同父节点
Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. According ...
- leetcode 235. Lowest Common Ancestor of a Binary Search Tree 236. Lowest Common Ancestor of a Binary Tree
https://www.cnblogs.com/grandyang/p/4641968.html http://www.cnblogs.com/grandyang/p/4640572.html 利用二 ...
- 235.236. Lowest Common Ancestor of a Binary (Search) Tree -- 最近公共祖先
235. Lowest Common Ancestor of a Binary Search Tree Given a binary search tree (BST), find the lowes ...
- [LeetCode] 235. Lowest Common Ancestor of a Binary Search Tree 二叉搜索树的最小共同父节点
Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BS ...
- leetcode 236. Lowest Common Ancestor of a Binary Tree
Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. According ...
- (medium)LeetCode 236.Lowest Common Ancestor of a Binary Tree
Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. According ...
随机推荐
- scikit-learn安装
1.依赖包: Cython.rose.numpy.scipy.lapack.atlas http://blog.chinaunix.net/uid-22488454-id-3978860.html
- 深入理解c++中char*与wchar_t*与string以及wstring之间的相互转换
本篇文章是对c++中的char*与wchar_t*与string以及wstring之间的相互转换进行了详细的分析介绍,需要的朋友参考下-复制代码 代码如下: #ifndef USE_H_ ...
- c++调用matlab生成的Dll动态连接库
点击打开链接http://download.csdn.net/detail/nuptboyzhb/4228429 c++调用matlab生成的Dll动态连接库 实验平台: matlab 7.0(R ...
- 使用rsyslog+loganalzey收集日志显示客户端ip
http://www.ituring.com.cn/article/128536 rsyslog localhost 转发 http://bigsec.net/one/tool/rsyslog.htm ...
- photoshop:把路径存储为形状
这个其实跟定义画笔步骤是一样的 路径存储为自定义形状 1.用路径选择工具(快捷键A),选中路径 2.菜单:编辑->定义自定形状 3.选择自定义形状工具(快捷键U),可以看到刚定义的形状 把当前形 ...
- ArcGIS学习记录—KMZ KML与SHP文件互相转换
1.在google earth中绘制边界 工具栏中选择"Add Polygon".随意绘制一个多边形. 右击添加的图层名(左侧)保存位置为,选择保存为kmz或kml文件. ...
- 【原创】oracle的tpc-c测试及方法
大家好,很高兴来到博客园分享自己的所见所得.希望和大家多多交流,共同进步. 本文重点在于简介使用BenchmarkSQL对oracle进行tpcc的测试步骤,只是一个简单入门的过程. 开源测试工具:B ...
- Python:urllib和urllib2的区别
urllib和urllib2都是处理url请求的两个模块,但是相互之间存在不同,不能相互取代 urllib2可以接受一个Reuqest类的实例来设置URL请求的headers,urllib仅可以接受U ...
- Exel 利用模板导出方法
#region Exel导出方法 [MaxuniAuthAttribute(Roles = "sysroles")] public void OrderExport(string ...
- 8.21 usaco
summary:6 bzoj1692://后缀数组就行了O(nlogn)c[30]会RE...注意!!! #include<cstdio> #include<cstring> ...