[力扣题解] 236. 二叉树的最近公共祖先

文章目录

题目:236. 二叉树的最近公共祖先

思路

代码

MyMethod

用深度搜索的思想(好吧,前序、中序、后序都是深搜思想),保存寻找路径,看看找到2个节点的路径的重合部分,就可以找到最近公共祖先;

该方法实际上不需要遍历整棵树;

cpp 复制代码
class Solution {
public:
    vector<TreeNode*> path;
    vector<vector<TreeNode*>> result;
    void travel(TreeNode* cur, TreeNode* target)
    {   
        if(cur == NULL)
        {
            return;
        }
        // 中
        if(cur->val == target->val)
        {
            result.push_back(path);
            return;
        }
        // 左
        if(cur->left)
        {
            path.push_back(cur->left);
            travel(cur->left, target);
            path.pop_back();
        }
        // 右
        if(cur->right)
        {
            path.push_back(cur->right);
            travel(cur->right, target);
            path.pop_back();
        }
    }
    TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) 
    {
        int i, j, min_length;
        path.push_back(root);
        travel(root, q);
        path.clear();
        path.push_back(root);
        
        travel(root, p);
        /*
        for(i = 0; i < result.size(); i++)
        {
            for(j = 0; j < result[i].size(); j++)
            {
                cout << result[i][j]->val << ", ";
            }
            cout << endl;
        }*/
        // cout << "result[0].size() : " << result[0].size() << endl;
        // cout << "result[1].size() : " << result[1].size() << endl;
        min_length = min(result[0].size(), result[1].size());
        for(i = 0; i < min_length; i++)
        {
            if(result[0][i]->val == result[1][i]->val)
            {
                continue;
            }
            else
            {
                break;
            }
        }
        // cout << "i : " << i << "result[0][i] : " << result[0][i]->val;
        return result[0][i-1];
    }
};

Method 2

后序遍历,左、右、中。中间节点处理左右子树有没有找到孩子。

后序遍历很恰当的模拟了回溯的过程,需要遍历整棵树;

cpp 复制代码
class Solution {
public:
    TreeNode* travel(TreeNode* cur, TreeNode* p, TreeNode* q)
    {
        if(cur == p || cur == q || cur == NULL)
        {
            return cur;
        }
        // 左
        TreeNode* left = travel(cur->left, p, q);
        // 右
        TreeNode* right = travel(cur->right, p, q);
        // 中
        if(left && right) // 找到了
        {
            return cur;
        }
        else if(left && right == NULL)
        {
            return left;
        }
        else // (left == NULL && right)
        {
            return right;
        }

    }

    TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
        return travel(root, p, q);
    }
};
相关推荐
lifallen14 分钟前
Paimon 原子提交实现
java·大数据·数据结构·数据库·后端·算法
lixzest15 分钟前
C++ Lambda 表达式详解
服务器·开发语言·c++·算法
EndingCoder17 分钟前
搜索算法在前端的实践
前端·算法·性能优化·状态模式·搜索算法
丶小鱼丶21 分钟前
链表算法之【合并两个有序链表】
java·算法·链表
不吃洋葱.1 小时前
前缀和|差分
数据结构·算法
是白可可呀3 小时前
LeetCode 169. 多数元素
leetcode
jz_ddk3 小时前
[实战]调频(FM)和调幅(AM)信号生成(完整C语言实现)
c语言·算法·信号处理
CloudAce云一4 小时前
谷歌云代理商:谷歌云TPU/GPU如何加速您的AI模型训练和推理
算法
轻语呢喃4 小时前
每日LeetCode : 杨辉三角
javascript·后端·算法
YuTaoShao5 小时前
【LeetCode 热题 100】148. 排序链表——(解法二)分治
java·算法·leetcode·链表