LintCode-剑指Offer-(88)最近公共祖先
来源:互联网 发布:网络安全法考试单选 编辑:程序博客网 时间:2024/06/05 09:45
class Solution {public: /** * @param root: The root of the binary search tree. * @param A and B: two nodes in a Binary. * @return: Return the least common ancestor(LCA) of the two nodes. */ TreeNode *lowestCommonAncestor(TreeNode *root,TreeNode *A,TreeNode *B) { // write your code here if (root==NULL)return NULL; if (root->left==NULL&&root->right==NULL)return root; vector<TreeNode*>vta = getlist(root,A); vector<TreeNode*>vtb = getlist(root,B); int i = 0; while (i<vta.size()&&i<vtb.size()&&vta[i]==vtb[i]){ i++; } if (i==vta.size()&&i<vtb.size())return A; if (i<vta.size()&&i==vtb.size())return B; return vta[i-1]; } vector<TreeNode*> getlist(TreeNode* root,TreeNode* A){ vector<TreeNode*> vta; while (root!=A){ vta.push_back(root); if (IsChild(root->left,A)==true){ root = root->left; } else if (IsChild(root->right,A)==true){ root = root->right; } } return vta; } bool IsChild(TreeNode* root,TreeNode* a){ if (root==NULL)return false; if (root==a)return true; return IsChild(root->left,a)|IsChild(root->right,a); }};
0 0
- LintCode-剑指Offer-(88)最近公共祖先
- lintcode-最近公共祖先-88
- 【剑指offer】最近公共祖先
- LintCode-最近公共祖先
- LintCode 最近公共祖先
- LintCode:最近公共祖先
- LintCode:最近公共祖先
- lintcode-最近公共祖先
- 最近公共祖先,lintcode
- lintcode, 最近公共祖先
- 最近公共祖先-LintCode
- lintcode--最近公共祖先
- lintcode----最近公共祖先
- [LintCode]88.最近公共祖先
- lintcode lowest-common-ancestor 最近公共祖先
- [Lintcode]Lowest Common Ancestor 最近公共祖先
- 【面试题】剑指Offer-50-求两个节点的最近公共祖先
- LintCode:M-最近祖先
- HDU-5533-Dancing Stars on Me【2015长春赛区】
- 各种版本控制工具的使用
- 每次开机后MySQL服务都不见了需要重新开mysqld-debug的解决办法
- 维护堆性质中,堆(完全二叉树)中i结点的子树大小至多为2n/3的证明
- multiple reactors + thread pool(one loop per thread + threadpool)(突发I/O与密集计算)
- LintCode-剑指Offer-(88)最近公共祖先
- 【基于Jsoup】Android_App暴走笑话开发(二)
- java ssh整合出的错java.lang.NoSuchMethodError antlr.collections.AST.getLine()I
- opencv 图片转视频
- 代码实现利用inf文件安装硬件驱动
- Maven +SpringMVC 搭建时候遇到视图view跳转失败问题
- Qt5 UDP开发
- object-c之音频视频的播放
- nyc_taxi_data处理手记