Class TreeAncestor

  • All Implemented Interfaces:

    
    public final class TreeAncestor
    
                        

    1483 - Kth Ancestor of a Tree Node.

    Hard

    You are given a tree with n nodes numbered from 0 to n - 1 in the form of a parent array parent where parent[i] is the parent of <code>i<sup>th</sup></code> node. The root of the tree is node 0. Find the <code>k<sup>th</sup></code> ancestor of a given node.

    The <code>k<sup>th</sup></code> ancestor of a tree node is the <code>k<sup>th</sup></code> node in the path from that node to the root node.

    Implement the TreeAncestor class:

    • TreeAncestor(int n, int[] parent) Initializes the object with the number of nodes in the tree and the parent array.

    • int getKthAncestor(int node, int k) return the <code>k<sup>th</sup></code> ancestor of the given node node. If there is no such ancestor, return -1.

    Example 1:

    Input "TreeAncestor", "getKthAncestor", "getKthAncestor", "getKthAncestor" [[7, -1, 0, 0, 1, 1, 2, 2], 3, 1, 5, 2, 6, 3]

    Output: null, 1, 0, -1

    Explanation:

    TreeAncestor treeAncestor = new TreeAncestor(7, -1, 0, 0, 1, 1, 2, 2);

    treeAncestor.getKthAncestor(3, 1); // returns 1 which is the parent of 3

    treeAncestor.getKthAncestor(5, 2); // returns 0 which is the grandparent of 5

    treeAncestor.getKthAncestor(6, 3); // returns -1 because there is no such ancestor

    Constraints:

    • <code>1 <= k <= n <= 5 * 10<sup>4</sup></code>

    • parent.length == n

    • parent[0] == -1

    • 0 &lt;= parent[i] &lt; n for all 0 &lt; i &lt; n

    • 0 &lt;= node &lt; n

    • There will be at most <code>5 * 10<sup>4</sup></code> queries.

    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
    • Field Summary

      Fields 
      Modifier and Type Field Description
    • Enum Constant Summary

      Enum Constants 
      Enum Constant Description
    • Method Summary

      Modifier and Type Method Description
      final Integer getKthAncestor(Integer node, Integer k)
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait