二叉搜索树的最近公共祖先

该代码实现了一个C++类Solution,包含一个方法lowestCommonAncestor,用于在二叉树中找到给定两个节点的最低公共祖先。方法通过递归遍历树结构,检查每个节点是否是目标节点或其祖先。
摘要由CSDN通过智能技术生成

在这里插入图片描述

#include <iostream>
#include <unordered_map>
#include <unordered_set>
#include <string>
#include <vector>
#include <algorithm>
#include <map>
#include <set>
#include <assert.h>
#include <stack>
#include <queue>

using namespace std;
struct TreeNode {
    int val;
    TreeNode* left;
    TreeNode* right;
    TreeNode() : val(0), left(nullptr), right(nullptr) {}
    TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
    TreeNode(int x, TreeNode* left, TreeNode* right) : val(x), left(left), right(right) {}
    
};

class Solution {
private:
    bool findChild(TreeNode* root, TreeNode* child) {
        if (!root || !child) {
            return false;
        }

        if (root->left == child || root->right == child) {
            return true;
        }
        
        bool ret = findChild(root->left, child);
        if (ret) {
            return true;
        }

        return findChild(root->right, child);
    }

    void findAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
        if (!root) {
            return;
        }
        if (root == p ) {
            bool ret = findChild(p, q);
            if (ret) {
                nodes.clear();
                nodes.push_back(p);
                find = true;
                return;
            }
            return;
        }
        else if (root == q) {
            bool ret = findChild(q, p);
            if (ret) {
                nodes.clear();
                nodes.push_back(q);
                find = true;
                return;
            }
            return;
        }

        if ( (root->left == p && root->right == q)
            || (root->left == q && root->right == p)) {
            nodes.clear();
            nodes.push_back(root);
            find = true;
            return;
        }

        if (root->left == p ) {
            bool ret = findChild(p, q);
            if (ret) {
                nodes.clear();
                nodes.push_back(p);
                find = true;
                return;
            }
            ret = findChild(root->right, q);
            if (ret) {
                nodes.clear();
                nodes.push_back(root);
                find = true;
                return;
            }
            
            nodes.push_back(root);
            return;
        }
        else if (root->right == p) {
            bool ret = findChild(p, q);
            if (ret) {
                nodes.clear();
                nodes.push_back(p);
                find = true;
                return;
            }
            ret = findChild(root->left, q);
            if (ret) {
                nodes.clear();
                nodes.push_back(root);
                find = true;
                return;
            }
            
            nodes.push_back(root);
            return;
        }
        else if (root->left == q ) {
            bool ret = findChild(q, p);
            if (ret) {
                nodes.clear();
                nodes.push_back(q);
                find = true;
                return;
            }
            ret = findChild(root->right, p);
            if (ret) {
                nodes.clear();
                nodes.push_back(root);
                find = true;
                return;
            }

            nodes.push_back(root);
            return;
        }
        else if (root->right == q) {
            bool ret = findChild(q, p);
            if (ret) {
                nodes.clear();
                nodes.push_back(q);
                find = true;
                return;
            }
            ret = findChild(root->left, p);
            if (ret) {
                nodes.clear();
                nodes.push_back(root);
                find = true;
                return;
            }
            nodes.push_back(root);
            return;
        }

        findAncestor(root->left, p, q);
        if (!find) {
            findAncestor(root->right, p, q);
        }
    }
    vector< TreeNode*> nodes;
    bool find = false;
public:
    TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
        if (!root) {
            return NULL;
        }
        TreeNode* ret = NULL;
        
        findAncestor(root, p, q);

        while (!find) {
            assert(nodes.size() == 2);
            p = nodes[0];
            q = nodes[1];
            nodes.clear();
            findAncestor(root, p, q);
        }

        assert(nodes.size() == 1);

        return nodes[0];
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值