classTreeAncestor:def__init__(self,n:int,parent:List[int]):self.p=[[-1]*18for_inrange(n)]fori,fainenumerate(parent):self.p[i][0]=faforjinrange(1,18):foriinrange(n):ifself.p[i][j-1]==-1:continueself.p[i][j]=self.p[self.p[i][j-1]][j-1]defgetKthAncestor(self,node:int,k:int)->int:foriinrange(17,-1,-1):ifk>>i&1:node=self.p[node][i]ifnode==-1:breakreturnnode# Your TreeAncestor object will be instantiated and called as such:# obj = TreeAncestor(n, parent)# param_1 = obj.getKthAncestor(node,k)
classTreeAncestor{privateint[][]p;publicTreeAncestor(intn,int[]parent){p=newint[n][18];for(vare:p){Arrays.fill(e,-1);}for(inti=0;i<n;++i){p[i][0]=parent[i];}for(intj=1;j<18;++j){for(inti=0;i<n;++i){if(p[i][j-1]==-1){continue;}p[i][j]=p[p[i][j-1]][j-1];}}}publicintgetKthAncestor(intnode,intk){for(inti=17;i>=0;--i){if(((k>>i)&1)==1){node=p[node][i];if(node==-1){break;}}}returnnode;}}/** * Your TreeAncestor object will be instantiated and called as such: * TreeAncestor obj = new TreeAncestor(n, parent); * int param_1 = obj.getKthAncestor(node,k); */
classTreeAncestor{public:TreeAncestor(intn,vector<int>&parent){p=vector<vector<int>>(n,vector<int>(18,-1));for(inti=0;i<n;++i){p[i][0]=parent[i];}for(intj=1;j<18;++j){for(inti=0;i<n;++i){if(p[i][j-1]==-1){continue;}p[i][j]=p[p[i][j-1]][j-1];}}}intgetKthAncestor(intnode,intk){for(inti=17;~i;--i){if(k>>i&1){node=p[node][i];if(node==-1){break;}}}returnnode;}private:vector<vector<int>>p;};/** * Your TreeAncestor object will be instantiated and called as such: * TreeAncestor* obj = new TreeAncestor(n, parent); * int param_1 = obj->getKthAncestor(node,k); */
typeTreeAncestorstruct{p[][18]int}funcConstructor(nint,parent[]int)TreeAncestor{p:=make([][18]int,n)fori,fa:=rangeparent{p[i][0]=faforj:=1;j<18;j++{p[i][j]=-1}}forj:=1;j<18;j++{fori:=rangep{ifp[i][j-1]==-1{continue}p[i][j]=p[p[i][j-1]][j-1]}}returnTreeAncestor{p}}func(this*TreeAncestor)GetKthAncestor(nodeint,kint)int{fori:=17;i>=0;i--{ifk>>i&1==1{node=this.p[node][i]ifnode==-1{break}}}returnnode}/** * Your TreeAncestor object will be instantiated and called as such: * obj := Constructor(n, parent); * param_1 := obj.GetKthAncestor(node,k); */
classTreeAncestor{privatep:number[][];constructor(n:number,parent:number[]){constp=newArray(n).fill(0).map(()=>newArray(18).fill(-1));for(leti=0;i<n;++i){p[i][0]=parent[i];}for(letj=1;j<18;++j){for(leti=0;i<n;++i){if(p[i][j-1]===-1){continue;}p[i][j]=p[p[i][j-1]][j-1];}}this.p=p;}getKthAncestor(node:number,k:number):number{for(leti=17;i>=0;--i){if(((k>>i)&1)===1){node=this.p[node][i];if(node===-1){break;}}}returnnode;}}/** * Your TreeAncestor object will be instantiated and called as such: * var obj = new TreeAncestor(n, parent) * var param_1 = obj.getKthAncestor(node,k) */
publicclassTreeAncestor{privateint[][]p;publicTreeAncestor(intn,int[]parent){p=newint[n][];for(inti=0;i<n;i++){p[i]=newint[18];for(intj=0;j<18;j++){p[i][j]=-1;}}for(inti=0;i<n;++i){p[i][0]=parent[i];}for(intj=1;j<18;++j){for(inti=0;i<n;++i){if(p[i][j-1]==-1){continue;}p[i][j]=p[p[i][j-1]][j-1];}}}publicintGetKthAncestor(intnode,intk){for(inti=17;i>=0;--i){if(((k>>i)&1)==1){node=p[node][i];if(node==-1){break;}}}returnnode;}}/** * Your TreeAncestor object will be instantiated and called as such: * TreeAncestor obj = new TreeAncestor(n, parent); * int param_1 = obj.GetKthAncestor(node,k); */