LeetCode刷题_c++版-236. 二叉树的最近公共祖先

法一:下标法

大的下标除以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:

    float getIndex(TreeNode* root, float index, int value){
    
    
        if(root == NULL) return -1;
        if(value == root->val) {
    
    
            cout<< index << endl;
            return index;
        }
        float returnValue = getIndex(root->left, 2*index, value);
        if(returnValue > 0)return returnValue;
        returnValue = getIndex(root->right, 2*index+1, value);
        if(returnValue > 0)return returnValue;
        return -1;
    }

    TreeNode* getNode(TreeNode* root, float index, float targetIndex, int &statute){
    
    
        
        if(root == NULL) return NULL;
        //cout<<"value="<<root->val <<" index="<< index<<endl;
        if(index == targetIndex){
    
    
            statute = 1;
            return root;
        }
        TreeNode* tmpNode = getNode(root->left, 2*index, targetIndex, statute);
        if(statute == 1)return tmpNode;
        tmpNode = getNode(root->right, 2*index+1, targetIndex, statute);
        if(statute == 1)return tmpNode;
        return NULL;
            
    }

    TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
    
    
        float index1 = getIndex(root, 1, p->val);
        float index2 = getIndex(root, 1, q->val);
        while(index1>1 && index2>1){
    
    
            if(index1 == index2) break;
            if(index1 > index2) index1 = floor(index1/2);
            else index2 = floor(index2/2);
        }
        cout<<"index1="<< index1 << " index2="<<index2<<endl;
        if(index1 == 1 || index2 == 1) return root;
        
        else{
    
    
            int statute = 0;
            return getNode(root, 1, index1, statute);
        }
        
    }
};

在这里插入图片描述

法二:利用堆栈

/**
 * 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:
    //将根节点到该点的路径push到栈中
    void getPath(vector<TreeNode*>& path, int& statute,TreeNode* p, int targetValue){
    
    
        if(statute == 1 || p == NULL) return;
        
        if(p->val == targetValue) {
    
    
            //cout<<" push "<<p->val;
            path.push_back(p);
            statute = 1;
            return;
        }
        path.push_back(p);
        getPath(path, statute, p->left,  targetValue);
        if(statute == 1) return;
        getPath(path, statute, p->right,  targetValue);
        if(statute == 1) return;
        //未找到目标节点,回溯
        path.pop_back();
        return;

        
    }

    TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
    
    
        vector<TreeNode*> path_p;
        vector<TreeNode*> path_q;
        //标志是否找到该元素
        int statute = -1;
        getPath(path_p, statute, root, p->val);
        statute = -1;
        getPath(path_q, statute, root, q->val);
        
        while(path_p.size()>1 && path_q.size()>1){
    
    
            int pPathLength = path_p.size();
            int qPathLength = path_q.size();
            //cout<<endl<<" plength="<<pPathLength<<" qlength="<<qPathLength<<endl;
            if(path_p[pPathLength-1]->val == path_q[qPathLength - 1]->val) {
    
    
                //cout<<"ptop="<<path_p[pPathLength-1]->val<<" qtop="<<path_q[qPathLength - 1]->val;
                break;
            }
            //如果找到公共路径,两栈的长度一定是一样长的
            //将长度较长的栈pop栈顶元素
            if (pPathLength >= qPathLength) {
    
    
                path_p.pop_back();
                pPathLength--;
            }
            else if (pPathLength < qPathLength) {
    
    
                path_q.pop_back();
                qPathLength--;
            }
        }
        if(path_p.size() == 1 || path_q.size() == 1) return root;
        else return path_p[path_p.size() - 1];
        
    }
};

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_44343355/article/details/128889960