原题题目
代码实现(首刷自解)
struct TreeNode* ans;
bool visit(struct TreeNode* root, struct TreeNode* p, struct TreeNode* q)
{
if(!root)
return false;
bool left = visit(root->left,p,q);
bool right = visit(root->right,p,q);
if((root == p || root == q) && (left || right) || left && right)
{
ans = root;
return true;
}
if(root == p || root == q)
return true;
return left || right;
}
struct TreeNode* lowestCommonAncestor(struct TreeNode* root, struct TreeNode* p, struct TreeNode* q) {
ans = NULL;
visit(root,p,q);
return ans;
}
代码实现(二刷自解 C++)
class Solution {
public:
TreeNode* ret;
bool visit(TreeNode* p,TreeNode* q,TreeNode* root)
{
if(!root) return false;
bool left = visit(p,q,root->left);
bool right = visit(p,q,root->right);
if(root == p || root == q)
{
if(left || right) ret = root;
return true;
}
else if(left && right)
ret = root;
return left || right;
}
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
ret = nullptr;
if(p == root || q == root) return root;
if(visit(p,q,root->left) && !ret) return root;
else visit(p,q,root->right);
return ret;
}
};
代码实现(三刷自解 DAY 146 C++)
class Solution {
public:
TreeNode* ret = nullptr;
bool visit(TreeNode* root,TreeNode* p,TreeNode* q)
{
if(!root) return false;
bool left = visit(root->left,p,q);
bool right = visit(root->right,p,q);
bool judge = (root->val == p->val || root->val == q->val);
if(right && left || ((right || left) && judge)) ret = root;
return left || right || judge;
}
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
if(root->val == p->val || root->val == q->val) return root;
visit(root->left,p,q);
visit(root->right,p,q);
if(!ret) ret = root;
return ret;
}
};
代码实现(四刷自解 DAY 209 C++)
class Solution {
public:
bool helper(TreeNode* root,TreeNode* p,TreeNode* q,TreeNode** ret)
{
if(!root) return false;
bool left = helper(root->left,p,q,ret);
bool right = helper(root->right,p,q,ret);
bool judge = (root == p || root == q);
if(judge && (left || right) || left && right) *ret = root;
return (left || right || judge);
}
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
if(root == p || root == q) return root;
TreeNode* left_ancestor = nullptr,*right_ancestor = nullptr;
helper(root->left,p,q,&left_ancestor);
helper(root->right,p,q,&right_ancestor);
if(left_ancestor) return left_ancestor;
if(right_ancestor) return right_ancestor;
return root;
}
};
代码实现(五刷自解 DAY 270 C++)
class Solution {
public:
TreeNode* ret = nullptr;
bool Visit(TreeNode* root, TreeNode* target) {
if (!root) return false;
if (target == root) return true;
return Visit(root->left, target) || Visit(root->right, target);
}
bool FindAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
if (!root) return false;
bool left = FindAncestor(root->left, p, q);
bool right = FindAncestor(root->right, p, q);
if (ret) return true;
bool has = (root == p || root == q);
if ((left && right) || has && (left || right)) ret = root;
return has || left || right;
}
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
if (p == root || q == root) return root;
bool p_in_left = Visit(root->left, p);
bool q_in_left = Visit(root->left, q);
if (p_in_left && !q_in_left || !p_in_left && q_in_left) return root;
if(p_in_left) {
FindAncestor(root->left, p, q);
} else {
FindAncestor(root->right, p, q);
}
return ret;
}
};
代码实现(六刷自解 DAY 2 Golang)
var ret *TreeNode
func hasroot(root, other *TreeNode) bool {
if root == nil || other == nil {
return false
}
return root.Val == other.Val || hasroot(root.Left, other) || hasroot(root.Right, other)
}
func helper(root, p, q *TreeNode) bool {
if root == nil {
return false
}
left, right := helper(root.Left, p, q), helper(root.Right, p, q)
judge := root.Val == p.Val || root.Val == q.Val
if left && right || (left || right) && judge {
ret = root
return true
}
return left || right || judge
}
func lowestCommonAncestor(root, p, q *TreeNode) *TreeNode {
if root == q || root == p {
return root
}
if hasroot(root.Left, p) && hasroot(root.Right, q) ||
hasroot(root.Left, q) && hasroot(root.Right, p) {
return root
}
helper(root, p, q)
return ret
}