[LeetCode] Lowest Common Ancestor of a Binary Search Tree
2015-07-11 17:27
274 查看
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 BST.
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
经典问题!
方法一:找到两个节点的路径,然后根据路径找LCA。
方法二:根据BST的性质,两个节点a,b的公共袓先c一定满足a <= c <= b 或者 a >= c >= b。
Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST.
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).”
_______6______ / \ ___2__ ___8__ / \ / \ 0 _4 7 9 / \ 3 5
For example, the lowest common ancestor (LCA) of nodes
2and
8is
6. Another example is LCA of nodes
2and
4is
2, since a node can be a descendant of itself according to the LCA definition.
经典问题!
方法一:找到两个节点的路径,然后根据路径找LCA。
/** * 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 getPath(TreeNode *root, TreeNode *p, vector<TreeNode*> &path) { TreeNode *tmp = root; while (tmp != p) { path.push_back(tmp); if (tmp->val > p->val) tmp = tmp->left; else tmp = tmp->right; } path.push_back(p); } TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) { vector<TreeNode*> path1, path2; getPath(root, p, path1); getPath(root, q, path2); TreeNode *res = root; int idx = 0; while (idx < path1.size() && idx < path2.size()) { if (path1[idx] != path2[idx]) break; else res = path1[idx++]; } return res; } };
方法二:根据BST的性质,两个节点a,b的公共袓先c一定满足a <= c <= b 或者 a >= c >= b。
/** * 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: TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) { TreeNode *cur = root; while (cur != NULL) { if (cur->val > p->val && cur->val > q->val) cur = cur->left; else if (cur->val < p->val && cur->val < q->val) cur = cur->right; else return cur; } return cur; } };
相关文章推荐
- 1.4 Java发展简史
- 黑马程序员——Foundation---NSSet,NSDictionary,NSNumber,NSDate,NSValue
- poj 3678 Katu Puzzle 2-SAT
- VS2010+Qt5+OSG3.0开发环境搭建
- php中常用的几个魔法常量
- 在drawable下的xml文件中layer-list的用二:在layer-list下的item标签中使用shape画图,以及backgroun和src的区别
- 嵌入式项目开发调试流程,常见问题,解决方法
- 使用uiautomator做UI测试
- 关于程序批量处理人工做的数据。。。。
- VC中动态改变控件和对话框字体(大小)
- audio_device模块分析
- video_capture模块分析
- 二叉树的六种遍历方式
- ubuntu安装下的下载工具,uget安装
- AIX系统维护手册一.系统盘镜像
- poj 3180 The Cow Prom 强连通分量
- python学习笔记——循环语句条件语句
- video_render模块分析
- poj2955解题报告
- linux centos 下安装中文输入法