当前位置:首页 » 《随便一记》 » 正文

最近公共祖先三种类型汇总(漫画版)_2021dragon的博客

29 人参与  2022年05月26日 13:25  分类 : 《随便一记》  评论

点击全文阅读


文章目录

  • 最近公共祖先定义
  • 查找最近公共祖先
    • 三叉链
    • 二叉搜索树
    • 普通二叉树

在这里插入图片描述
在这里插入图片描述

最近公共祖先定义

在这里插入图片描述
在这里插入图片描述

查找最近公共祖先

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

三叉链

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
代码如下:

//三叉链
struct TreeNode {
    int val;
    TreeNode *left;
    TreeNode *right;
	TreeNode *parent;
    TreeNode(int x) : val(x), left(NULL), right(NULL), parent(NULL) {}
};
class Solution {
public:
	TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
		TreeNode* curp = p, *curq = q; //用于遍历p、q结点的祖先结点
		int lenp = 0, lenq = 0; //分别记录p、q结点各自的祖先结点个数
		//统计p结点的祖先结点个数
		while (curp != root)
		{
			lenp++;
			curp = curp->parent;
		}
		//统计q结点的祖先结点个数
		while (curq != root)
		{
			lenq++;
			curq = curq->parent;
		}
		//longpath和shortpath分别标记祖先路径较长和较短的结点
		TreeNode* longpath = p, *shortpath = q;
		if (lenp < lenq)
		{
			longpath = q;
			shortpath = p;
		}
		//p、q结点祖先结点个数的差值
		int count = abs(lenp - lenq);
		//先让longpath往上走count个结点
		while (count--)
		{
			longpath = longpath->parent;
		}
		//再让longpath和shortpath同时往上走,此时第一个相同的结点便是最近公共祖先结点
		while (longpath != shortpath)
		{
			longpath = longpath->parent;
			shortpath = shortpath->parent;
		}
		return longpath; //返回最近公共祖先结点
	}
};

二叉搜索树

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
代码如下:

//搜索二叉树
struct TreeNode {
	int val;
	TreeNode *left;
	TreeNode *right;
	TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
class Solution {
public:
	TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
		if (p->val == root->val || q->val == root->val) //p、q结点中某一个结点的值等于根结点的值,则根结点就是这两个结点的最近公共祖先
			return root;
		if (p->val < root->val&&q->val < root->val) //p、q结点的值都小于根结点的值,说明这两个结点的最近公共祖先在该树的左子树当中
			return lowestCommonAncestor(root->left, p, q);
		else if (p->val > root->val&&q->val > root->val) //p、q结点的值都大于根结点的值,说明这两个结点的最近公共祖先在该树的右子树当中
			return lowestCommonAncestor(root->right, p, q);
		else //p、q结点的值一个比根小一个比根大,说明根就是这两个结点的最近公共祖先
			return root;
	}
};

普通二叉树

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
代码如下:

//普通二叉树
struct TreeNode {
	int val;
	TreeNode *left;
	TreeNode *right;
	TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
class Solution {
public:
	bool Find(TreeNode* root, TreeNode* x)
	{
		if (root == nullptr) //空树,查找失败
			return false;
		if (root == x) //查找成功
			return true;

		return Find(root->left, x) || Find(root->right, x); //在左子树找到了或是右子树找到了,都说明该结点在该树当中
	}
	TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
		if (p == root || q == root) //p、q结点中某一个结点为根结点,则根结点就是这两个结点的最近公共祖先
			return root;
		//判断p、q结点是否在左右子树
		bool IspInLeft, IspInRight, IsqInLeft, IsqInRight;
		IspInLeft = Find(root->left, p);
		IspInRight = !IspInLeft;
		IsqInLeft = Find(root->left, q);
		IsqInRight = !IsqInLeft;

		if (IspInLeft&&IsqInLeft) //p、q结点都在左子树,说明这两个结点的最近公共祖先也在左子树当中
			return lowestCommonAncestor(root->left, p, q);
		else if (IspInRight&&IsqInRight) //p、q结点都在右子树,说明这两个结点的最近公共祖先也在右子树当中
			return lowestCommonAncestor(root->right, p, q);
		else //p、q结点一个在左子树一个在右子树,说明根就是这两个结点的最近公共祖先
			return root;
	}
};

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
看着似乎不太好理解,来看看下面的动图演示:
在这里插入图片描述
代码如下:

//普通二叉树
struct TreeNode {
	int val;
	TreeNode *left;
	TreeNode *right;
	TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
class Solution {
public:
	bool FindPath(TreeNode* root, TreeNode* x, stack<TreeNode*>& path)
	{
		if (root == nullptr)
			return false;
		path.push(root); //该结点可能是路径当中的结点,先入栈

		if (root == x) //该结点是最终结点,查找结束
			return true;

		if (FindPath(root->left, x, path)) //在该结点的左子树找到了最终结点,查找结束
			return true;
		if (FindPath(root->right, x, path)) //在该结点的右子树找到了最终结点,查找结束
			return true;

		path.pop(); //在该结点的左右子树均没有找到最终结点,该结点不可能是路径当中的结点,该结点出栈
		return false; //在该结点处查找失败
	}
	TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
		stack<TreeNode*> pPath, qPath;
		FindPath(root, p, pPath); //将从根到p结点的路径存放到pPath当中
		FindPath(root, q, qPath); //将从根到q结点的路径存放到qPath当中
		//longpath和shortpath分别标记长路径和短路径
		stack<TreeNode*>* longPath = &pPath, *shortPath = &qPath;
		if (pPath.size() < qPath.size())
		{
			longPath = &qPath;
			shortPath = &pPath;
		}
		//让longPath先弹出差值个数据
		int count = longPath->size() - shortPath->size();
		while (count--)
		{
			longPath->pop();
		}
		//longPath和shortPath一起弹数据,直到两个栈顶的结点相同
		while (longPath->top() != shortPath->top())
		{
			longPath->pop();
			shortPath->pop();
		}
		return longPath->top(); //返回这个相同的结点,即最近公共祖先
	}
};

在这里插入图片描述


点击全文阅读


本文链接:http://zhangshiyu.com/post/40804.html

结点  子树  祖先  
<< 上一篇 下一篇 >>

  • 评论(0)
  • 赞助本站

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

最新文章

  • 最新章节_笔趣阁(方黎萧明德)方黎萧明德小说(心跳谋杀论)在线畅读阅读
  • 明月独照成双高分完本(顾明玉齐远修)全书免费_(顾明玉齐远修)明月独照成双高分完本后续(顾明玉齐远修)
  • 真千金的妈,女凭母贵的我内容精选(娇娇顾静怡江稚雅)
  • 雪上春霜在线温观意陆一然完本_雪上春霜在线(温观意陆一然)
  • 男友车祸断腿时,我和新欢在车内拥吻高分言情_陈行简江若***新书_小说后续在线阅读_无删减免费完结_
  • 完结文重生高考填报志愿时,我放任全班上大专好文分享列表_完结文重生高考填报志愿时,我放任全班上大专好文分享(颜禾晚宋津年叶淼淼)
  • 陆寒澈沈秋晚小说完本+外篇(相逢难逃别离)畅享阅读
  • 雪上春霜删减内容修复版本(温观意陆一然)_雪上春霜删减内容修复版本(温观意陆一然)
  • 骤雨淋落晚星(林晚星裴临川)全书浏览_骤雨淋落晚星全书浏览
  • 全书浏览重生高考填报志愿时我放任全班上大专+后续(叶淼淼宋津年颜禾晚)_重生高考填报志愿时我放任全班上大专+后续(叶淼淼宋津年颜禾晚)全书结局
  • 他的甜撩藏不住附加黎初谢时晏:结局+番外免费品鉴(他的甜撩藏不住)全本浏览阅读连载中
  • 全文他死遁后逼疯清冷女权臣+完结+后续(苏煜顾宛柔)列表_全文他死遁后逼疯清冷女权臣+完结+后续

    关于我们 | 我要投稿 | 免责申明

    Copyright © 2020-2022 ZhangShiYu.com Rights Reserved.豫ICP备2022013469号-1