- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Print all nodes at distance k from a given node in C++
In this problem, we are given a binary tree, a target node and an integer K. We have to print all the nodes of the tree that are at a distance K from the target node.
Binary Tree is a special tree whose each node has at max two nodes (one/two/none).
Let’s take an example to understand the problem
K = 2
Target node: 9
Output −
5 1 3.
Explanation −
The distance can be taken for node a higher, lower or at the same level. So, we will return nodes accordingly.
To solve this problem we have to understand what are the types of nodes that are K distance away from the target node.
From the above exam, we can see the k distant nodes can be in the subtree of the target node (5 and 1) or anywhere in the subtree of the ancestor of the target node(3).
Now, the method to find the solution of the first case is by traversing the subtree of the target node, and check if the distance of a node from the target is K. If yes print the node.
For the second case, we have to check the ancestor nodes and the subtree of these ancestors for the target and print all at distance K from it.
The below program will show the implementation of our solution −
Example
#include <iostream> using namespace std; struct node { int data; struct node *left, *right; }; void printSubtreeNodes(node *root, int k) { if (root == NULL || k < 0) return; if (k==0){ cout<<root->data<<"\t"; return; } printSubtreeNodes(root->left, k-1); printSubtreeNodes(root->right, k-1); } int printKNodes(node* root, node* target , int k){ if (root == NULL) return -1; if (root == target){ printSubtreeNodes(root, k); return 0; } int dl = printKNodes(root->left, target, k); if (dl != -1){ if (dl + 1 == k) cout<<root->data<<"\t"; else printSubtreeNodes(root->right, k-dl-2); return 1 + dl; } int dr = printKNodes(root->right, target, k); if (dr != -1){ if (dr + 1 == k) cout << root->data << endl; else printSubtreeNodes(root->left, k-dr-2); return 1 + dr; } return -1; } node *insertNode(int data){ node *temp = new node; temp->data = data; temp->left = temp->right = NULL; return temp; } int main(){ node * root = insertNode(6); root->left = insertNode(3); root->right = insertNode(9); root->left->right = insertNode(4); root->right->left = insertNode(8); root->right->right = insertNode(10); root->right->right->left = insertNode(5); root->right->right->right = insertNode(1); node * target = root->right; int K = 2; cout<<"Nodes at distance "<<K<<" from the target node are :\n"; printKNodes(root, target, K); return 0; }
Output
Nodes at distance 2 from the target n tode are − 5 1 3
- Related Articles
- Print all nodes that are at distance k from a leaf node in C++
- All Nodes Distance K in Binary Tree in C++
- Find all reachable nodes from every node present in a given set in C++
- Print all nodes in a binary tree having K leaves in C++
- Print Leaf Nodes at a given Level in C language
- Print nodes of linked list at given indexes in C language
- Print all nodes between two given levels in Binary Tree in C++
- Find distance from root to given node in a binary tree in C++
- XOR of all the nodes in the sub-tree of the given node in C++
- Print all full nodes in a Binary Tree in C++
- Print all leaf nodes of a binary tree from right to left in C++
- How to remove all child nodes from a parent node using jQuery?
- Print all internal nodes of a Binary tree in C++
- Print cousins of a given node in Binary Treein C++
- Python Program to Find All Nodes Reachable from a Node using BFS in a Graph
