# Count of nodes which are at a distance X from root and leaves

Given two integers **N** and **X**, where **N** is the number of nodes in an almost complete binary tree. The task is to find:

- The number of nodes that are at
**X**distance from the root. - The number of nodes that are at
**X**distance from any leaf in its subtree.

**Note:** A Complete binary tree is a binary tree in which every level, except possibly the last, is completely filled and all nodes are as far left as possible.

**Examples:**

Input:N = 6, X = 0Output:1 3 Complete Binary Tree of 6 nodes is 1 / \ 2 3 / \ / 4 5 6 Nodes that are at 0 distance from root = 1 (root itself). Nodes that are at 0 distance from any of the leaf = 3 (all the leaves of the tree)Input:N = 13, X = 1Output:2 4 Complete Binary Tree of 13 nodes. 1 / \ 2 3 / \ / \ 4 5 6 7 / \ / \ / \ 8 9 10 11 12 13 Nodes that are at 0 distance from root = 2 (node no. 2 and 3) Nodes that are at 0 distance from any of the leaf = 4 (node no. 4, 5, 6 and 3)

**Approach:**

- Finding the number of nodes that are x distance away from the root is simple. We simply print the number of nodes at x-th height. We know that in a complete binary tree every level is complete, except possibly the last and all nodes are as far left as possible. So, the height
**h**of a complete binary tree is calculated as**floor(log2(n))**where**n**is the total number of nodes. Also, the number of nodes at i-th height will be**2**and the number of nodes that are at last level (i.e at height h) =^{i}**2**, where^{h}– (max_total_nodes – n)**2**are maximum nodes at height^{h}**h**. - Finding the number of nodes that are x distance away from any leaf in its subtree is a little bit tricky. It’s about observing the fact that if we have
**l**leaf nodes then,**ceil(l/2)**nodes are**1**unit distance away from leaves,**ceil(l/4)**nodes are**2**units distance away from leaves …. till**ceil(l/2**nodes are^{i})**i**units distance away from leaves. We will first find the count of leaf nodes and apply the above method to find nodes that are x distance away from leaf.

Below is the implementation of the above approach:

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to find the count ` `// of the required nodes ` `void` `countNodes(` `int` `N, ` `int` `X) ` `{ ` ` ` ` ` `// Height of the complete binary ` ` ` `// tree with n nodes ` ` ` `int` `height = ` `floor` `(log2(N)); ` ` ` ` ` `// If X > height then no node ` ` ` `// can be present at that level ` ` ` `if` `(X > height) { ` ` ` `cout << ` `"0\n0"` `; ` ` ` `return` `; ` ` ` `} ` ` ` ` ` `// Corner case ` ` ` `if` `(N == 1) { ` ` ` `cout << ` `"1\n1"` `; ` ` ` `return` `; ` ` ` `} ` ` ` ` ` `// Maximum total nodes that are possible ` ` ` `// in complete binary tree with height h ` ` ` `int` `max_total_nodes = (1 << (height + 1)) - 1; ` ` ` ` ` `// Nodes at the last level ` ` ` `int` `nodes_last_level = (1 << height) - (max_total_nodes - N); ` ` ` ` ` `// To store the count of nodes ` ` ` `// x dist away from root ` ` ` `int` `from_root; ` ` ` ` ` `// To store the count of nodes ` ` ` `// x dist away from leaf ` ` ` `int` `from_leaf; ` ` ` ` ` `// If X = h then print nodes at last level ` ` ` `// else nodes at Xth level ` ` ` `if` `(X == height) ` ` ` `from_root = nodes_last_level; ` ` ` ` ` `// 2^X ` ` ` `else` ` ` `from_root = 1 << X; ` ` ` ` ` `// Number of left leaf nodes at (h-1)th level ` ` ` `// observe that if nodes are not present at last level ` ` ` `// then there are a/2 leaf nodes at (h-1)th level ` ` ` `int` `left_leaf_nodes = ((1 << height) - nodes_last_level) / 2; ` ` ` ` ` `// If X = h then print leaf nodes at the last h level ` ` ` `// + leaf nodes at (h-1)th level ` ` ` `if` `(X == 0) { ` ` ` `from_leaf = nodes_last_level + left_leaf_nodes; ` ` ` `} ` ` ` `else` `{ ` ` ` ` ` `// First calculate nodes for leaves present at height h ` ` ` `int` `i = X; ` ` ` ` ` `while` `(nodes_last_level > 1 && i > 0) { ` ` ` `nodes_last_level = ` `ceil` `((` `float` `)nodes_last_level / (` `float` `)2); ` ` ` `i--; ` ` ` `} ` ` ` ` ` `from_leaf = nodes_last_level; ` ` ` ` ` `// Then calculate nodes for leaves present at height h-1 ` ` ` `i = X; ` ` ` `while` `(left_leaf_nodes > 1 && i > 0) { ` ` ` `left_leaf_nodes = ` `ceil` `((` `float` `)left_leaf_nodes / (` `float` `)2); ` ` ` `i--; ` ` ` `} ` ` ` ` ` `// Add both the resuls ` ` ` `from_leaf += left_leaf_nodes; ` ` ` `} ` ` ` ` ` `cout << from_root << endl ` ` ` `<< from_leaf; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `N = 38, X = 3; ` ` ` ` ` `countNodes(N, X); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

8 3

## Recommended Posts:

- Tree with N nodes and K leaves such that distance between farthest leaves is minimized
- Print nodes at k distance from root
- Print nodes at k distance from root | Iterative
- Print all nodes in a binary tree having K leaves
- Find distance from root to given node in a binary tree
- Iterative program to find distance of a node from root
- Check if two nodes are in same subtree of the root node
- Root to leaf path with maximum distinct nodes
- Print the path common to the two paths from the root to the two given nodes
- Remove nodes on root to leaf paths of length < K
- Sum of nodes on the longest path from root to leaf node
- Find the path from root to the given nodes of a tree for multiple queries
- Print path from root to all nodes in a Complete Binary Tree
- Print all nodes at distance k from a given node
- Print all nodes that are at distance k from a leaf node

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.