Given a tree of N nodes and an integer K, each node is numbered between 1 and N. The task is to find the number of pairs of ideal nodes in a tree.
A pair of nodes (a, b) is called ideal if
- a is an ancestor of b.
- And, abs(a – b) ≤ K
K = 2 Output: 4 (1, 2), (1, 3), (3, 4), (3, 5) are such pairs. Input: Consider the graph in example 1 and k = 3 Output: 6 (1, 2), (1, 3), (1, 4), (3, 4), (3, 5), (3, 6) are such pairs.
Approach: First, we need to traverse the tree using DFS so we need to find the root node, the node without a parent. As we traverse each node we will store it in a data structure to keep track of all the ancestors for the next node. Before doing that, get the number of the node’s ancestors in the range [presentNode – k, presentNode + k] then add it to the total pairs.
We need a data structure which can:
- Insert a node as we traverse the tree.
- Remove a node as we return.
- Give the number of nodes within the range [presentNode – k, PresentNode + k] which were stored.
Binary indexed tree fulfills the above three operations.
We can do the above 3 operations by initializing all the index values of the BIT to 0 and then:
- Insert a node by updating the index of that node to 1.
- Remove a node by updating the index of that node to 0.
- Get the number of similar pairs of the ancestor of that node by querying for the sum of the range [presentNode – k, PresentNode + k]
Below is the implementation of the above approach:
- Common nodes in the inorder sequence of a tree between given two nodes in O(1) space
- Construct XOR tree by Given leaf nodes of Perfect Binary Tree
- Minimum difference between any two weighted nodes in Sum Tree of the given Tree
- Find the shortest distance between any pair of two different good nodes
- Remove all leaf nodes from a Generic Tree or N-ary Tree
- Queries to find the maximum Xor value between X and the nodes of a given level of a perfect binary tree
- Find the path from root to the given nodes of a tree for multiple queries
- Find distance between two nodes in the given Binary tree for Q queries
- Query to find the maximum and minimum weight between two nodes in the given tree using LCA.
- Count the number of nodes at a given level in a tree using DFS
- Count the nodes in the given Tree whose weight is a Perfect Number
- Splitting starting N nodes into new Circular Linked List while preserving the old nodes
- Queries to find distance between two nodes of a Binary tree
- Find maximum among all right nodes in Binary Tree
- Find maximum count of duplicate nodes in a Binary Search Tree
- Queries to find distance between two nodes of a Binary tree - O(logn) method
- Maximum sub-tree sum in a Binary Tree such that the sub-tree is also a BST
- Print all nodes between two given levels in Binary Tree
- Print the nodes with a prime degree in given Prufer sequence of a Tree
- Print the Forests of a Binary Tree after removal of given Nodes
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.