算法练习----力扣每日一题------7

原题链接

1483. 树节点的第 K 个祖先 - 力扣(LeetCode)

题目解析

要求编写一个TreeAncestor类,需要为其写两个函数。该类是一个无规律的多叉树,多叉树的父节点一定是0号节点

  1. TreeAncestor(int n, vector<int>& parent)

n为parent的长度,也为多叉树的节点个数,parent中存的是每个节点的父节点的编号,例如parent[3] = 1代表3号节点的父节点是1号节点

  1. int getKthAncestor(int node, int k)

node为节点序号,k是一个计数器,该函数要求返回node节点的第k辈祖先节点的序号,如果不存在返回-1

  • 1 <= k <= n <= 5 * 10^4
  • parent[0] == -1 表示编号为 0 的节点是根节点。
  • 对于所有的 0 < i < n0 <= parent[i] < n 总成立
  • 0 <= node < n
  • 至多调用 5 * 10^4 次getKthAncestor

解题:

两种不合题意的解法:

直接暴力求解:

超时

cpp 复制代码
class TreeAncestor {
public:
    TreeAncestor(int n, vector<int>& parent) {
        tmp = {parent.begin(),parent.end()};
    }
    
    int getKthAncestor(int node, int k) {
        
        while(k--)
        {
            if(node == 0)
                return -1;
            node = tmp[node];
        }
        return node;
    }
    vector<int> tmp;
};

构造函数时间复杂度o(n)空间复杂度o(n)

get函数时间复杂度o(k) 空间复杂度o(1)

假设调用a次get:总时间复杂度o(k*a+n)空间复杂度o(n)

优化暴力

超空间

在暴力解法中每次调用get都会从node开始一直向前算,这些计算很可能在之前的遍历中都算过了。如果我们在执行循环之前把所有的情况都算出来,之后调用get时就可以直接得到。

cpp 复制代码
class TreeAncestor {
public:
	TreeAncestor(int n, vector<int>& parent) {
		tmp = { parent.begin(),parent.end() };
		r.resize(tmp.size());
		for (int i = 1; i < tmp.size(); i++)
		{
			for (int j = i; j >= 0; j = tmp[j])
			{
				r[i].push_back(j);
			}
		}
	}

	int getKthAncestor(int node, int k) {
		if (k >= r[node].size())
			return -1;
		else
			return r[node][k];
	}
	vector<int> tmp;
	vector<vector<int>> r;
};

构造函数时间复杂度o(n*n)空间复杂度o(n*n)

get函数时间复杂度o(1) 空间复杂度o(1)

假设调用a次get:总时间复杂度o(n*n)空间复杂度o(n*n)

倍增记录法

既然优化暴力法会超空间限制,想办法只取一部分的数据进行记录即可。

cpp 复制代码
class TreeAncestor 
{
public:
	const int num = 16;
	vector<vector<int>>a;
	TreeAncestor(int n, vector<int>& parent) 
	{
		a.resize(n, vector<int>(num, -1));
		for (int i = 0; i < n; i++)
		{
			a[i][1] = parent[i];
		}
		for (int j = 2; j < num; j++)
		{
			for (int i = 0; i < n; i++)
			{
				if (a[i][j-1] != -1)
					a[i][j] = a[a[i][j - 1]][j - 1];
			}
		}
	}

	int getKthAncestor(int node, int k) {
		int tmp = 0;
		while (k != 0)
		{
			if (node == -1)
				return -1;
			int a1 = 1,a2=0;
			while (a1 <= k)
				a1 *= 2,a2++;
			k -= a1 / 2;
			node = a[node][ a2 ];
		}
		return node;
	}
};

构造函数时间复杂度o(n*log(n))空间复杂度o(n*log(n))

get函数时间复杂度o(log(k)) 空间复杂度o(1)

假设调用a次get:总时间复杂度o(n*log(n)+a*log(k))空间复杂度o(n*log(n))


感谢观看!!!!

相关推荐
浅念-6 小时前
递归解题指南:LeetCode经典题全解析
数据结构·算法·leetcode·职场和发展·排序算法·深度优先·递归
Kiling_07046 小时前
Java集合进阶:Set与Collections详解
算法·哈希算法
智者知已应修善业6 小时前
【51单片机89C51及74LS273、74LS244组成】2022-5-28
c++·经验分享·笔记·算法·51单片机
洛水水7 小时前
【力扣100题】33.验证二叉搜索树
算法·leetcode·职场和发展
SimpleLearingAI7 小时前
聚类算法详解
算法·数据挖掘·聚类
刀法如飞8 小时前
Go 字符串查找的 20 种实现方式,用不同思路解决问题
算法·面试·程序员
Dlrb12119 小时前
C语言-指针数组与数组指针
c语言·数据结构·算法·指针·数组指针·指针数组·二级指针
WL_Aurora9 小时前
Python 算法基础篇之集合
python·算法
平行侠10 小时前
A15 工业路由器IP前缀高速检索与内存压缩系统
网络·tcp/ip·算法
阿旭超级学得完11 小时前
C++11包装器(function和bind)
java·开发语言·c++·算法·哈希算法·散列表