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 pair 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**

**Examples:**

Input:K = 2Output:4 (1, 2), (1, 3), (3, 4), (3, 5) are such pairs.Input:Consider the graph in example 1 and k = 3Output: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:

## C++

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` `#define N 100005 ` ` ` `int` `n, k; ` ` ` `// Adjacency list ` `vector<` `int` `> al[N]; ` `long` `long` `Ideal_pair; ` `long` `long` `bit[N]; ` `bool` `root_node[N]; ` ` ` `// bit : bit array ` `// i and j are starting and ` `// ending index INCLUSIVE ` `long` `long` `bit_q(` `int` `i, ` `int` `j) ` `{ ` ` ` `long` `long` `sum = 0ll; ` ` ` `while` `(j > 0) { ` ` ` `sum += bit[j]; ` ` ` `j -= (j & (j * -1)); ` ` ` `} ` ` ` `i--; ` ` ` `while` `(i > 0) { ` ` ` `sum -= bit[i]; ` ` ` `i -= (i & (i * -1)); ` ` ` `} ` ` ` `return` `sum; ` `} ` ` ` `// bit : bit array ` `// n : size of bit array ` `// i is the index to be updated ` `// diff is (new_val - old_val) ` `void` `bit_up(` `int` `i, ` `long` `long` `diff) ` `{ ` ` ` `while` `(i <= n) { ` ` ` `bit[i] += diff; ` ` ` `i += i & -i; ` ` ` `} ` `} ` ` ` `// DFS function to find ideal pairs ` `void` `dfs(` `int` `node) ` `{ ` ` ` `Ideal_pair += bit_q(max(1, node - k), ` ` ` `min(n, node + k)); ` ` ` `bit_up(node, 1); ` ` ` `for` `(` `int` `i = 0; i < al[node].size(); i++) ` ` ` `dfs(al[node][i]); ` ` ` `bit_up(node, -1); ` `} ` ` ` `// Function for initialisation ` `void` `initalise() ` `{ ` ` ` `Ideal_pair = 0; ` ` ` `for` `(` `int` `i = 0; i <= n; i++) { ` ` ` `root_node[i] = ` `true` `; ` ` ` `bit[i] = 0LL; ` ` ` `} ` `} ` ` ` `// Function to add an edge ` `void` `Add_Edge(` `int` `x, ` `int` `y) ` `{ ` ` ` `al[x].push_back(y); ` ` ` `root_node[y] = ` `false` `; ` `} ` ` ` `// Function to find number of ideal pairs ` `long` `long` `Idealpairs() ` `{ ` ` ` `// Find root of the tree ` ` ` `int` `r = -1; ` ` ` `for` `(` `int` `i = 1; i <= n; i++) ` ` ` `if` `(root_node[i]) { ` ` ` `r = i; ` ` ` `break` `; ` ` ` `} ` ` ` ` ` `dfs(r); ` ` ` ` ` `return` `Ideal_pair; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `n = 6, k = 3; ` ` ` ` ` `initalise(); ` ` ` ` ` `// Add edges ` ` ` `Add_Edge(1, 2); ` ` ` `Add_Edge(1, 3); ` ` ` `Add_Edge(3, 4); ` ` ` `Add_Edge(3, 5); ` ` ` `Add_Edge(3, 6); ` ` ` ` ` `// Function call ` ` ` `cout << Idealpairs(); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation of the approach ` `N ` `=` `100005` `Ideal_pair ` `=` `0` ` ` `# Adjacency list ` `al ` `=` `[[] ` `for` `i ` `in` `range` `(` `100005` `)] ` `bit ` `=` `[` `0` `for` `i ` `in` `range` `(N)] ` `root_node ` `=` `[` `0` `for` `i ` `in` `range` `(N)] ` ` ` `# bit : bit array ` `# i and j are starting and ` `# ending index INCLUSIVE ` `def` `bit_q(i, j): ` ` ` `sum` `=` `0` ` ` `while` `(j > ` `0` `): ` ` ` `sum` `+` `=` `bit[j] ` ` ` `j ` `-` `=` `(j & (j ` `*` `-` `1` `)) ` ` ` ` ` `i ` `-` `=` `1` ` ` `while` `(i > ` `0` `): ` ` ` `sum` `-` `=` `bit[i] ` ` ` `i ` `-` `=` `(i & (i ` `*` `-` `1` `)) ` ` ` `return` `sum` ` ` `# bit : bit array ` `# n : size of bit array ` `# i is the index to be updated ` `# diff is (new_val - old_val) ` `def` `bit_up(i, diff): ` ` ` `while` `(i <` `=` `n): ` ` ` `bit[i] ` `+` `=` `diff ` ` ` `i ` `+` `=` `i & ` `-` `i ` ` ` `# DFS function to find ideal pairs ` `def` `dfs(node, x): ` ` ` `Ideal_pair ` `=` `x ` ` ` `Ideal_pair ` `+` `=` `bit_q(` `max` `(` `1` `, node ` `-` `k), ` ` ` `min` `(n, node ` `+` `k)) ` ` ` `bit_up(node, ` `1` `) ` ` ` `for` `i ` `in` `range` `(` `len` `(al[node])): ` ` ` `Ideal_pair ` `=` `dfs(al[node][i], Ideal_pair) ` ` ` `bit_up(node, ` `-` `1` `) ` ` ` ` ` `return` `Ideal_pair ` ` ` `# Function for initialisation ` `def` `initalise(): ` ` ` `Ideal_pair ` `=` `0` `; ` ` ` `for` `i ` `in` `range` `(n ` `+` `1` `): ` ` ` `root_node[i] ` `=` `True` ` ` `bit[i] ` `=` `0` ` ` `# Function to add an edge ` `def` `Add_Edge(x, y): ` ` ` `al[x].append(y) ` ` ` `root_node[y] ` `=` `False` ` ` `# Function to find number of ideal pairs ` `def` `Idealpairs(): ` ` ` ` ` `# Find root of the tree ` ` ` `r ` `=` `-` `1` ` ` `for` `i ` `in` `range` `(` `1` `, n ` `+` `1` `, ` `1` `): ` ` ` `if` `(root_node[i]): ` ` ` `r ` `=` `i ` ` ` `break` ` ` ` ` `Ideal_pair ` `=` `dfs(r, ` `0` `) ` ` ` ` ` `return` `Ideal_pair ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `'__main__'` `: ` ` ` `n ` `=` `6` ` ` `k ` `=` `3` ` ` ` ` `initalise() ` ` ` ` ` `# Add edges ` ` ` `Add_Edge(` `1` `, ` `2` `) ` ` ` `Add_Edge(` `1` `, ` `3` `) ` ` ` `Add_Edge(` `3` `, ` `4` `) ` ` ` `Add_Edge(` `3` `, ` `5` `) ` ` ` `Add_Edge(` `3` `, ` `6` `) ` ` ` ` ` `# Function call ` ` ` `print` `(Idealpairs()) ` ` ` `# This code is contributed by ` `# Surendra_Gangwar ` |

*chevron_right*

*filter_none*

**Output:**

6

## Recommended Posts:

- Find the shortest distance between any pair of two different good nodes
- Find maximum among all right nodes in Binary Tree
- Find distance between two nodes in the given Binary tree for Q queries
- Queries to find distance between two nodes of a Binary tree
- Number of special nodes in an n-ary tree
- Find the path from root to the given nodes of a tree for multiple queries
- Query to find the maximum and minimum weight between two nodes in the given tree using LCA.
- Level with maximum number of nodes using DFS in a N-ary tree
- Count the number of nodes at a given level in a tree using DFS
- 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
- Queries to find the maximum Xor value between X and the nodes of a given level of a perfect binary tree
- Count the nodes in the given Tree whose weight is a Perfect Number
- Number of leaf nodes in the subtree of every node of an n-ary tree
- 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
- Remove all leaf nodes from a Generic Tree or N-ary Tree
- Minimum difference between any two weighted nodes in Sum Tree of the given Tree
- Find the number of distinct pairs of vertices which have a distance of exactly k in a tree
- Find number of edges that can be broken in a tree such that Bitwise OR of resulting two trees are equal

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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.