倍增,LeetCode 1483. 树节点的第 K 个祖先

目录

一、题目

1、题目描述

2、接口描述

python3

cpp

3、原题链接

二、解题报告

1、思路分析

2、复杂度

3、代码详解

python3

cpp


一、题目

1、题目描述

给你一棵树,树上有 n 个节点,按从 0n-1 编号。树以父节点数组的形式给出,其中 parent[i] 是节点 i 的父节点。树的根节点是编号为 0 的节点。

树节点的第 k 个祖先节点是从该节点到根节点路径上的第 k 个节点。

实现 TreeAncestor 类:

  • TreeAncestor(int n, int[] parent) 对树和父数组中的节点数初始化对象。
  • getKthAncestor``(int node, int k) 返回节点 node 的第 k 个祖先节点。如果不存在这样的祖先节点,返回 -1

2、接口描述

python3
复制代码
python 复制代码
class TreeAncestor:

    def __init__(self, n: int, parent: List[int]):


    def getKthAncestor(self, node: int, k: int) -> int:



# Your TreeAncestor object will be instantiated and called as such:
# obj = TreeAncestor(n, parent)
# param_1 = obj.getKthAncestor(node,k)
cpp
复制代码
cpp 复制代码
class TreeAncestor {

  TreeAncestor(int n, List<int> parent) {
    
  }
  
  int getKthAncestor(int node, int k) {
    
  }
}

/**
 * Your TreeAncestor object will be instantiated and called as such:
 * TreeAncestor obj = TreeAncestor(n, parent);
 * int param1 = obj.getKthAncestor(node,k);
 */

3、原题链接

1483. 树节点的第 K 个祖先


二、解题报告

1、思路分析

关于ST表求LCA见:LCA算法-倍增算法-CSDN博客

比ST表求LCA板子题还要简单的题

我们预处理出f[u][i],代表u向上跳2^i层的祖先

然后对于查询向上第k个祖先我们只需要将k二进制拆分往上跳即可

2、复杂度

时间复杂度: 预处理O(nlogn) 查询:O(k)空间复杂度:O(nlogn)

3、代码详解

python3
复制代码
python 复制代码
class TreeAncestor:

    def __init__(self, n: int, parent: List[int]):
        H = n.bit_length()
        fa = [[0] * H] + [[p + 1] + [0] * (H - 1) for p in parent]
        for i in range(1, H):
            for j in range(1, n + 1):
                fa[j][i] = fa[fa[j][i - 1]][i - 1]
        self.fa = fa

    def getKthAncestor(self, x: int, k: int) -> int:
        x += 1
        fa = self.fa
        for i in range(k.bit_length()):
            if k >> i & 1:
                x = fa[x][i]
        return x - 1


# Your TreeAncestor object will be instantiated and called as such:
# obj = TreeAncestor(n, parent)
# param_1 = obj.getKthAncestor(node,k)
cpp
复制代码
cpp 复制代码
const int N = 5e4 + 10, H = 16;
int fa[N][H];
class TreeAncestor {
public:
    TreeAncestor(int n, vector<int>& parent) {
        for(int i = 1; i < n; i++)
            fa[i + 1][0] = parent[i] + 1;
        for(int i = 1, ed = 32 - __builtin_clz(n); i < ed; i++)
            for(int j = 1; j <= n; j++)
                fa[j][i] = fa[fa[j][i - 1]][i - 1];
    }
    
    int getKthAncestor(int x, int k) {
        ++x;
        for(int i = 32 - __builtin_clz(k); i >= 0; i--)
            if(k >> i & 1)
                x = fa[x][i];
        return x - 1;
    }
};

/**
 * Your TreeAncestor object will be instantiated and called as such:
 * TreeAncestor* obj = new TreeAncestor(n, parent);
 * int param_1 = obj->getKthAncestor(node,k);
 */
相关推荐
索迪迈科技18 小时前
Flink Task线程处理模型:Mailbox
java·大数据·开发语言·数据结构·算法·flink
元亓亓亓19 小时前
LeetCode热题100--230. 二叉搜索树中第 K 小的元素--中等
算法·leetcode·职场和发展
草莓熊Lotso19 小时前
《算法闯关指南:优选算法-双指针》--01移动零,02复写零
c语言·c++·经验分享·算法·leetcode
焜昱错眩..20 小时前
代码随想录算法训练营第三十九天|62.不同路径 63.不同路径ll
算法
焦耳加热1 天前
阿德莱德大学Nat. Commun.:盐模板策略实现废弃塑料到单原子催化剂的高值转化,推动环境与能源催化应用
人工智能·算法·机器学习·能源·材料工程
wan5555cn1 天前
多张图片生成视频模型技术深度解析
人工智能·笔记·深度学习·算法·音视频
u6061 天前
常用排序算法核心知识点梳理
算法·排序
蒋星熠1 天前
Flutter跨平台工程实践与原理透视:从渲染引擎到高质产物
开发语言·python·算法·flutter·设计模式·性能优化·硬件工程
小欣加油1 天前
leetcode 面试题01.02判定是否互为字符重排
数据结构·c++·算法·leetcode·职场和发展
3Cloudream1 天前
LeetCode 003. 无重复字符的最长子串 - 滑动窗口与哈希表详解
算法·leetcode·字符串·双指针·滑动窗口·哈希表·中等