Skip to main content
Admissions guidance, study banks, and career tools — get started on Shalyam

K-th Ancestor of a Node

A tree is given by a parent array (parent of the root is -1, nodes are 0-indexed). For each query (node, k) print the k-th ancestor of node, or -1 if it does not exist.

Constraints

1 <= n, q <= 5*10^4 1 <= k <= n

Discussion

No discussions yet.

Rewards · v2.1

Get Exclusive Insights with Every Issue

Join Shalyam Newsletter

Stay ahead in education, research, and innovation—straight to your inbox.