#include <stdio.h> struct TreeNode { TreeNode *left, *right; int el; explicit TreeNode(int _el) : left(NULL), right(NULL), el(_el) {} }; typedef TreeNode* Tree; /* prints all the descendent nodes which are k distance from t */ void printNodes(Tree t, int k) { if (t == NULL || k < 0) { return; }; if (k == 0) { printf ("%d", t->el); } else { printNodes(t->left, k - 1); printNodes(t->right, k - 1); } } /* prints all the nodes which are k distance from the given node n returns the distance of t from n */ int printNodes(Tree t, TreeNode *n, int k) { if (t == NULL) return -1; if (t == n) { printNodes(t, k); return 0; } int d; if (t->left) { d = printNodes(t->left, n, k); if (d >= 0) { d++; if (d == k) { printf("%d", t->el); } else { printNodes(t->right, k - d - 1); } return d; } } if (t->right) { d = printNodes(t->right, n, k); if (d >= 0) { d++; if (d == k) { printf("%d", t->el); } else { printNodes(t->left, k - d - 1); } return d; } } return -1; } /* Example Tree: 1 / \ 2 3 / \ / \ 4 5 6 7 / \ / 8 9 10 */ Tree newTree() { TreeNode *root = new TreeNode(1); root->left = new TreeNode(2); root->right = new TreeNode(3); root->left->left = new TreeNode(4); root->left->right = new TreeNode(5); root->right->left = new TreeNode(6); root->right->right = new TreeNode(7); root->left->left->left = new TreeNode(8); root->left->right->right = new TreeNode(9); root->right->right->left = new TreeNode(10); return root; } int main(int argc, char const *argv[]) { Tree t = newTree(); printNodes(t, t->left->right->right, 4); //prints nodes which are 4 distance away from 9 which are (8, 3) printf("\n"); printNodes(t, t, 3); //prints nodes which are 3 distance away from 1 which are (8, 9, 10) printf("\n"); printNodes(t, t->left->right, 4); //prints nodes which are 4 distance away from 5 which are (6, 7) printf("\n"); return 0; }

Advertisements
(function(){var c=function(){var a=document.getElementById("crt-1339930497");window.Criteo?(a.parentNode.style.setProperty("display","inline-block","important"),a.style.setProperty("display","block","important"),window.Criteo.DisplayAcceptableAdIfAdblocked({zoneid:388248,containerid:"crt-1339930497",collapseContainerIfNotAdblocked:!0,callifnotadblocked:function(){a.style.setProperty("display","none","important");a.style.setProperty("visbility","hidden","important")}})):(a.style.setProperty("display","none","important"),a.style.setProperty("visibility","hidden","important"))};if(window.Criteo)c();else{if(!__ATA.criteo.script){var b=document.createElement("script");b.src="//static.criteo.net/js/ld/publishertag.js";b.onload=function(){for(var a=0;a<__ATA.criteo.cmd.length;a++){var b=__ATA.criteo.cmd[a];"function"===typeof b&&b()}};(document.head||document.getElementsByTagName("head")[0]).appendChild(b);__ATA.criteo.script=b}__ATA.criteo.cmd.push(c)}})();
(function(){var c=function(){var a=document.getElementById("crt-1606818607");window.Criteo?(a.parentNode.style.setProperty("display","inline-block","important"),a.style.setProperty("display","block","important"),window.Criteo.DisplayAcceptableAdIfAdblocked({zoneid:837497,containerid:"crt-1606818607",collapseContainerIfNotAdblocked:!0,callifnotadblocked:function(){a.style.setProperty("display","none","important");a.style.setProperty("visbility","hidden","important")}})):(a.style.setProperty("display","none","important"),a.style.setProperty("visibility","hidden","important"))};if(window.Criteo)c();else{if(!__ATA.criteo.script){var b=document.createElement("script");b.src="//static.criteo.net/js/ld/publishertag.js";b.onload=function(){for(var a=0;a<__ATA.criteo.cmd.length;a++){var b=__ATA.criteo.cmd[a];"function"===typeof b&&b()}};(document.head||document.getElementsByTagName("head")[0]).appendChild(b);__ATA.criteo.script=b}__ATA.criteo.cmd.push(c)}})();