[力扣题解] 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);
    }
};
相关推荐
Gyoku Mint35 分钟前
深度学习×第4卷:Pytorch实战——她第一次用张量去拟合你的轨迹
人工智能·pytorch·python·深度学习·神经网络·算法·聚类
葫三生2 小时前
如何评价《论三生原理》在科技界的地位?
人工智能·算法·机器学习·数学建模·量子计算
拓端研究室4 小时前
视频讲解:门槛效应模型Threshold Effect分析数字金融指数与消费结构数据
前端·算法
随缘而动,随遇而安6 小时前
第八十八篇 大数据中的递归算法:从俄罗斯套娃到分布式计算的奇妙之旅
大数据·数据结构·算法
IT古董6 小时前
【第二章:机器学习与神经网络概述】03.类算法理论与实践-(3)决策树分类器
神经网络·算法·机器学习
Alfred king9 小时前
面试150 生命游戏
leetcode·游戏·面试·数组
水木兰亭9 小时前
数据结构之——树及树的存储
数据结构·c++·学习·算法
Jess0710 小时前
插入排序的简单介绍
数据结构·算法·排序算法
老一岁10 小时前
选择排序算法详解
数据结构·算法·排序算法
xindafu10 小时前
代码随想录算法训练营第四十二天|动态规划part9
算法·动态规划