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.
Subscribe to see which companies asked this question
分析:
本问题属于找规律的问题!
注意:本题给定的两个节点一定在这个二叉搜索树上,所以一定存在最低公共祖先。
如果当前节点r是q和p的祖先,一定满足如下条件:
1)p和q分居两颗子树
2)p和q中有一个就是当前节点,另一个居于某颗子树
那么其他情况呢?
3)p和q都在左子树或者右子树
而根据二叉搜索树的规则,我们可以通过比较三个节点的大小来判断谁在谁的那一边
class Solution { public: TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) { if(root->val < p->val && root->val < q->val)//都在右子树上 return lowestCommonAncestor(root->right,p,q); else if(root->val >p->val && root->val > q->val) //都在左子树上 return lowestCommonAncestor(root->left,p,q); else return root; } };
或者写成迭代的形式:
class Solution { public: TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) { TreeNode* curAnc=root; while(curAnc) { if(curAnc->val < p->val && curAnc->val < q->val)//都在右子树上 curAnc=curAnc->right; else if(curAnc->val >p->val && curAnc->val > q->val) //都在左子树上 curAnc=curAnc->left; else return curAnc; } } };
tips:
上面这两种写法,不允许两个给定节点不存在于二叉搜索树上或者一个存在而另一个不存在。
也没做什么特殊处理啊?尽然超过了这么多leetcode玩家
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 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.
Subscribe to see which companies asked this question
分析:
注意:本题给定的两个节点一定在这个二叉树上,所以一定存在最低公共祖先。
自己的答案:(纯手动,调试了半天才搞对)
思路就是记录从根节点达到p和q的两条路径,显然知道了两条路径之后,不相同的上一个节点就是最低公共祖先。
比如4和2的:
4的路径:3,5,2,4
2的路径:3,5,2
/** * 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* node, TreeNode* q,TreeNode* p,vector<TreeNode*> &result)//不要用值传递不然内存会大爆炸 { if(!qpath.empty() && !ppath.empty()) return; result.push_back(node); if(node==q) qpath=result; if(node==p) ppath=result; if(node->left) getPath(node->left,q,p,result); if(node->right) getPath(node->right,q,p,result); result.pop_back();//回溯去掉, } TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) { if(!root || !p || !q) return NULL; vector<TreeNode*> result;//找出两个包含这两个结点的路径 getPath(root,q,p,result); int i=1,j=1; while(i<qpath.size() && j<ppath.size()) { if(ppath[i]!=qpath[j]) return ppath[i-1]; if(i==qpath.size()-1)//这些返回条件一定要写清楚..........累觉不爱 return qpath[i]; if(j==ppath.size()-1) return ppath[j]; i++;j++; } return ppath[0]; } private: vector<TreeNode*> qpath; vector<TreeNode*> ppath; };
别人的答案:
class Solution { public: TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) { if (root==NULL || root == p || root == q) return root; TreeNode* left = lowestCommonAncestor(root->left, p, q); TreeNode* right = lowestCommonAncestor(root->right, p, q); return left==NULL ? right : !right ? left : root; } };
注:本博文为EbowTang原创,后续可能继续更新本文。如果转载,请务必复制本条信息!
原文地址:http://blog.csdn.net/ebowtang/article/details/50708367
原作者博客:http://blog.csdn.net/ebowtang