the art of
Algorithm
Notes on Analysis and Design



nodes at distance k from the root
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class Node:
	def __init__(self,key):
		self.data=key
		self.left=None
		self.right=None
#Function to print nodes at distance k from root of the tree
def kdistantnode(root,k):
	if root is None:
		return 0
	if k==0:
		return root.data
	else:
		kdistantnode(root.left,k-1)
		kdistantnode(root.right,k-1)
#Driver program to test above function
root = Node(1)
root.left = Node(2)
root.right = Node(3)
root.left.left = Node(4)
root.left.right = Node(5)
root.right.left = Node(8)
 
kdistantnode(root, 2)