[leetcode][dfs] Lowest Common Ancestor of a Binary Search Tree
来源:互联网 发布:探险家软件 编辑:程序博客网 时间:2024/04/27 15:36
题目:
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 2
and 8
is 6
. Another example is LCA of nodes 2
and 4
is 2
, 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 pathRootDownCore(TreeNode *root, TreeNode *p, TreeNode *q, vector<vector<TreeNode *> > &res, vector<TreeNode *> path){if (NULL == root) return;path.push_back(root);if (root == p){res[0] = path;if (!res[1].empty()) return;}else if (root == q){res[1] = path;if (!res[0].empty()) return;}if (root->left != NULL) pathRootDownCore(root->left, p, q, res, path);if (root->right != NULL) pathRootDownCore(root->right, p, q, res, path);}vector<vector<TreeNode *> > pathRootDown(TreeNode *root, TreeNode *p, TreeNode *q){vector<vector<TreeNode *> > res(2);vector<TreeNode *> dummy;pathRootDownCore(root, p, q, res, dummy);return res;}TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {if (NULL == root || NULL == p || NULL == q) return NULL;vector<vector<TreeNode *> > paths = pathRootDown(root, p, q);TreeNode *res = NULL;for (int i = 0; i < paths[0].size() && i < paths[1].size(); ++i){if (paths[0][i] == paths[1][i]) res = paths[0][i];else break;}return res;}};
时间复杂度是O(n+h),空间复杂度是O(h),n表示树的节点个数,h表示树的高度
思路二:如果在root的两边分别找到了p和q,那么root就是p和q的最低公共祖先,否则,它们的最低公共祖先就是在同一边找到p和q的那边的最低公共祖先 44ms
/** * 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) {if (NULL == root) return NULL;if (root == p || root == q) return root;TreeNode *pLeft = NULL, *pRight = NULL;if(root->left != NULL) pLeft = lowestCommonAncestor(root->left, p, q);if(root->right != NULL) pRight = lowestCommonAncestor(root->right, p, q);if (pLeft && pRight) return root;return pLeft != NULL ? pLeft : pRight;}};
思路三:利用BST的节点值域的大小关系 40ms
/** * 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* lowestCommonAncestorCore(TreeNode* root, TreeNode* p, TreeNode* q) {if (root == p || root == q || p->val < root->val && q->val > root->val) return root;if (p->val < root->val && q->val < root->val) return lowestCommonAncestorCore(root->left, p, q);return lowestCommonAncestorCore(root->right, p, q);}TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {if (NULL == root || NULL == p || NULL == q) return NULL;if (q->val < p->val) swap(p, q);return lowestCommonAncestorCore(root, p, q);//p->val < p->val}};
0 0
- [leetcode][dfs] Lowest Common Ancestor of a Binary Search Tree
- 235. Lowest Common Ancestor of a Binary Search Tree--dfs
- leetcode 235: Lowest Common Ancestor of a Binary Search Tree
- LeetCode 235. Lowest Common Ancestor of a Binary Search Tree
- leetcode: Lowest Common Ancestor of a Binary Search Tree
- [leetcode] 235.Lowest Common Ancestor of a Binary Search Tree
- 【LeetCode】235 Lowest Common Ancestor of a Binary Search Tree
- [LeetCode]235.Lowest Common Ancestor of a Binary Search Tree
- leetcode 235 Lowest Common Ancestor of a Binary Search Tree
- [leetcode] Lowest Common Ancestor of a Binary Search Tree
- LeetCode Lowest Common Ancestor of a Binary Search Tree
- leetCode(38):Lowest Common Ancestor of a Binary Search Tree
- leetcode-235-Lowest Common Ancestor of a Binary Search Tree
- [LeetCode]Lowest Common Ancestor of a Binary Search Tree
- Leetcode: Lowest Common Ancestor of a Binary Search Tree
- leetcode[235]:Lowest Common Ancestor of a Binary Search Tree
- Lowest Common Ancestor of a Binary Search Tree-leetcode
- 【leetcode】Lowest Common Ancestor of a Binary Search Tree
- 修复VS 2010的Help Library管理器和MSDN的安装过程
- maxsum
- 美团(三)之登录到主页面间的加载界面
- Scrapy初学
- 符号矩阵代入数据求值
- [leetcode][dfs] Lowest Common Ancestor of a Binary Search Tree
- Java多线程(二)、线程的生命周期和状态控制
- Servlet3.0入门学习(一)
- 关于mac/linux下vim的语法高亮与行号显示!
- 55分钟学会正则表达式(纠正了翻译中的一些错误)
- ZOJ Problem Set - 1092 Arbitrage (Floyd)
- cf Alter Board
- Android-动画
- CSS3文字立体效果