you give tree n node number 0 n 1 form parent array parent parenti parent ith node the root tree node 0 find kth ancestor give nodeThe kth ancestor tree node kth node path node root nodeImplement TreeAncestor classTreeAncestorint n int parent Initializes object number nod tree parent arrayint getKthAncestorint node int k return kth ancestor give node node if ancestor return 1 Example 1inputtreeancestor getkthancestor getkthancestor getkthancestor7 1 0 0 1 1 2 2 3 1 5 2 6 3outputnull 1 0 1explanationtreeancestor treeancestor new treeancestor7 1 0 0 1 1 2 2treeancestorgetkthancestor3 1 return 1 parent 3treeancestorgetkthancestor5 2 return 0 grandparent 5treeancestorgetkthancestor6 3 return 1 ancestor Constraints1 k n 5 104parentlength nparent0 10 parenti n 0 n0 node nThere 5 104 query