-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkthAncestorofaTreeNode.cpp
More file actions
38 lines (34 loc) · 1.12 KB
/
kthAncestorofaTreeNode.cpp
File metadata and controls
38 lines (34 loc) · 1.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
// Source: https://leetcode.com/problems/kth-ancestor-of-a-tree-node/
// Author: Miao Zhang
// Date: 2021-05-10
/*******************************************************************
* dp[i][j]: jth node, 2 ** i parent
* dp[i][j] = dp[i - 1][dp[i - 1][j]]
*******************************************************************/
class TreeAncestor {
public:
TreeAncestor(int n, vector<int>& parent) : maxlvl_(32 - __builtin_clz(n)), dp_(maxlvl_, vector<int>(n)) {
dp_[0] = parent;
for (int i = 1; i < maxlvl_; i++) {
for (int j = 0; j < n; j++) {
dp_[i][j] = dp_[i - 1][j] == -1 ? -1 : dp_[i - 1][dp_[i - 1][j]];
}
}
}
int getKthAncestor(int node, int k) {
for (int i = 0; i < maxlvl_ && node != -1; i++) {
if (k & (1 << i)) {
node = dp_[i][node];
}
}
return node;
}
private:
int maxlvl_;
vector<vector<int>> dp_;
};
/**
* Your TreeAncestor object will be instantiated and called as such:
* TreeAncestor* obj = new TreeAncestor(n, parent);
* int param_1 = obj->getKthAncestor(node,k);
*/