Here in this code, PrintNodesAtKDistance will first try to find the required node.
if(root.value == requiredNode)
When we find the desired node, we print all the child nodes at a distance K from this node.
Now our task is to print all the nodes that are in other branches (Go up and print). We return -1 until we find our desired node. When we get our desired node, we get lPath
or rPath >=0
. Now we need to print all the nodes that are at a distance of (lPath/rPath) -1
public void PrintNodes(Node Root, int requiredNode, int iDistance) { PrintNodesAtKDistance(Root, requiredNode, iDistance); } public int PrintNodesAtKDistance(Node root, int requiredNode, int iDistance) { if ((root == null) || (iDistance < 0)) return -1; int lPath = -1, rPath = -1; if(root.value == requiredNode) { PrintChildNodes(root, iDistance); return iDistance - 1; } lPath = PrintNodesAtKDistance(root.left, requiredNode, iDistance); rPath = PrintNodesAtKDistance(root.right, requiredNode, iDistance); if (lPath > 0) { PrintChildNodes(root.right, lPath - 1); return lPath - 1; } else if(lPath == 0) { Debug.WriteLine(root.value); } if(rPath > 0) { PrintChildNodes(root.left, rPath - 1); return rPath - 1; } else if (rPath == 0) { Debug.WriteLine(root.value); } return -1; } public void PrintChildNodes(Node aNode, int iDistance) { if (aNode == null) return; if(iDistance == 0) { Debug.WriteLine(aNode.value); } PrintChildNodes(aNode.left, iDistance - 1); PrintChildNodes(aNode.right, iDistance - 1); }
source share