题目 树节点的第 K 个祖先
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/kth-ancestor-of-a-tree-node
给你一棵树,树上有 n 个节点,按从 0 到 n-1 编号。树以父节点数组的形式给出,其中 parent[i] 是节点 i 的父节点。树的根节点是编号为 0 的节点。
请你设计并实现 getKthAncestor(int node, int k) 函数,函数返回节点 node 的第 k 个祖先节点。如果不存在这样的祖先节点,返回 -1 。
树节点的第 k 个祖先节点是从该节点到根节点路径上的第 k 个节点。
输入:
["TreeAncestor","getKthAncestor","getKthAncestor","getKthAncestor"]
[[7,[-1,0,0,1,1,2,2]],[3,1],[5,2],[6,3]]
输出:
[null,1,0,-1]
解释:
TreeAncestor treeAncestor = new TreeAncestor(7, [-1, 0, 0, 1, 1, 2, 2]);
treeAncestor.getKthAncestor(3, 1); // 返回 1 ,它是 3 的父节点
treeAncestor.getKthAncestor(5, 2); // 返回 0 ,它是 5 的祖父节点
treeAncestor.getKthAncestor(6, 3); // 返回 -1 因为不存在满足要求的祖先节点
提示:
1 <= k <= n <= 5*10^4
parent[0] == -1 表示编号为 0 的节点是根节点。
对于所有的 0 < i < n ,0 <= parent[i] < n 总成立
0 <= node < n
至多查询 5*10^4 次
基于动态规划
int N = 500010, M = 19; // M = (int) (Math.log(N) / Math.log(2) + 1)
int[][] dp = new int[N][M];
public TreeAncestor(int n, int[] father){
//初始化
for(int i = 0; i < n; i++){
Arrays.fill(dp[i], -1);
dp[i][0] = father[i];
}
for(int j = 1; j < M; j++){
for(int i = 0; i < n; i++){
if(dp[i][j - 1] != -1) dp[i][j] = dp[ dp[i][j - 1] ][j - 1];
}
}
}
public int getKthAncestor(int node, int k){
if(node == -1 || k == 0) return node;
int pos = 0, tmp = k;
//找到右边开始第一个1的位置 - 1
while (tmp > 0 && (tmp & 1) == 0){
tmp >>= 1;
pos++;
}
return getKthAncestor(dp[node][pos], k - (1 << pos));
}