剑指 Offer 68 - I. 二叉搜索树的最近公共祖先
题目描述
菜鸡思路:
- 二叉树,就是递归
- 二叉搜索树,还可以二分查找避免递归
菜鸡代码:
递归
/**
* 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 (p->val < root->val && q->val < root->val) {
root = lowestCommonAncestor(root->left, p, q);
}
if (p->val > root->val && q->val > root->val) {
root = lowestCommonAncestor(root->right, p, q);
}
return root;
}
};
二分查找
class Solution {
public:
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
int min = p->val < q->val ? p->val : q->val;
int max = p->val + q->val - min;
TreeNode *result = root;
while (root) {
if (root->val >= min && root->val <= max) {
return root;
} else if (root->val > max) {
root = root->left;
} else if (root->val < min) {
root = root->right;
}
}
return result;
}
};
剑指 Offer 68 - II. 二叉树的最近公共祖先
题目描述
菜鸡思路:
转化为二叉搜索树,利用上一道题的代码
菜鸡代码:
/**
* 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:
unordered_map<TreeNode*, int>myMap;
int count = 0;
void search(TreeNode* root) {
if (!root) {
return;
}
search(root->left);
myMap.insert({root, count++});
search(root->right);
}
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
search(root);
int min = myMap[p] < myMap[q] ? myMap[p] : myMap[q];
int max = myMap[p] + myMap[q] - min;
TreeNode *result = root;
while (root) {
if (myMap[root] >= min && myMap[root] <= max) {
return root;
} else if (myMap[root] > max) {
root = root->left;
} else if (myMap[root] < min) {
root = root->right;
}
}
return result;
}
};
大佬思路:
对于一颗树来说,如果节点p、q分别分布在root的左右子树中,那么节点p、q的最近公共祖先为root;如果p、q均只分布在root的一侧(左子树或者是右子树)中,我们则可以递归的处理相同的问题,即在一颗子树(root->left或者root->right)中判断节点p、q的最近公共祖先。
大佬代码:
class Solution {
public:
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
if(!root) {
return NULL;
}
if(root == p || root == q) {
return root;
}
auto left = lowestCommonAncestor(root->left, p, q);
auto right = lowestCommonAncestor(root->right, p, q);
if (left && right) {
return root;
}
if (left) {
return left;
}
return right;
}
};