Print nodes at k distance from root

Nodes at K distance from the root of the binary tree

Here given code implementation process.

// C program for 
// Print nodes at k distance from root
#include <stdio.h>
#include <stdlib.h>

// Tree Node
struct TreeNode
{
	int data;
	struct TreeNode *left;
	struct TreeNode *right;
};
// Binary Tree
struct BinaryTree
{
	struct TreeNode *root;
};
// Create new tree
struct BinaryTree *new_tree()
{
	// Create dynamic node
	struct BinaryTree *tree = (struct BinaryTree *) malloc(sizeof(struct BinaryTree));
	if (tree != NULL)
	{
		tree->root = NULL;
	}
	else
	{
		printf("Memory Overflow to Create tree Tree\n");
	}
	//return new tree
	return tree;
}
// This is creates and returns the new binary tree node
struct TreeNode *get_node(int data)
{
	// Create dynamic node
	struct TreeNode *new_node = (struct TreeNode *) malloc(sizeof(struct TreeNode));
	if (new_node != NULL)
	{
		//Set data and pointer values
		new_node->data = data;
		new_node->left = NULL;
		new_node->right = NULL;
	}
	else
	{
		//This is indicates, segmentation fault or memory overflow problem
		printf("Memory Overflow\n");
	}
	//return new node
	return new_node;
}
// Recursively display the nodes which are exist k distance from root
void printKthDistance(struct TreeNode *node, int k)
{
	if (node == NULL)
	{
		return;
	}
	if (k == 0)
	{
		// print k distance node
		printf(" %d", node->data);
	}
	else
	{
		// Visit left and right subtree
		printKthDistance(node->left, k - 1);
		printKthDistance(node->right, k - 1);
	}
}
// Handles the request to print every k distance node from root
void rootToKDistance(struct TreeNode *node, int k)
{
	if (node == NULL)
	{
		printf("Empty Binary Tree \n");
	}
	else if (k < 0)
	{
		printf("\n Invalid distance k : %d\n", k);
	}
	else
	{
		printf("\n Given k : %d\n", k);
		// Print nodes at k distance
		printKthDistance(node, k);
	}
}
int main(int argc, char
	const *argv[])
{
	struct BinaryTree *tree = new_tree();
	/*
	             4                            
	           /   \    
	         -4     7    
	         / \     \               
	        2   3     12
	       /   / \    / 
	      1   10  8  5
	     /    /       \
	    3    9         12 

	    -----------------
	    Binary tree

	*/
	tree->root = get_node(4);
	tree->root->left = get_node(-4);
	tree->root->left->right = get_node(3);
	tree->root->left->right->left = get_node(10);
	tree->root->left->right->left->left = get_node(9);
	tree->root->left->right->right = get_node(8);
	tree->root->left->left = get_node(2);
	tree->root->left->left->left = get_node(1);
	tree->root->left->left->left->left = get_node(3);
	tree->root->right = get_node(7);
	tree->root->right->right = get_node(12);
	tree->root->right->right->left = get_node(5);
	tree->root->right->right->left->right = get_node(5);
	// Test case
	rootToKDistance(tree->root, 4);
	rootToKDistance(tree->root, 2);
	rootToKDistance(tree->root, 0);
	return 0;
}

input

 Given k : 4
 3 9 5
 Given k : 2
 2 3 12
 Given k : 0
 4
/*
    Java Program
    Print nodes at k distance from root
*/
// Binary Tree node
class TreeNode
{
	public int data;
	public TreeNode left;
	public TreeNode right;
	public TreeNode(int data)
	{
		// Set node value
		this.data = data;
		this.left = null;
		this.right = null;
	}
}
public class BinaryTree
{
	public TreeNode root;
	public BinaryTree()
	{
		// Set initial tree root to null
		this.root = null;
	}
	// Recursively display the nodes which are exist k distance from root
	public void printKthDistance(TreeNode node, int k)
	{
		if (node == null)
		{
			return;
		}
		if (k == 0)
		{
			// print k distance node
			System.out.print(" " + node.data);
		}
		else
		{
			// Visit left and right subtree
			printKthDistance(node.left, k - 1);
			printKthDistance(node.right, k - 1);
		}
	}
	// Handles the request to print every k distance node from root
	public void rootToKDistance(int k)
	{
		if (this.root == null)
		{
			System.out.print("Empty Binary Tree \n");
		}
		else if (k < 0)
		{
			System.out.println("\n Invalid distance k : " + k);
		}
		else
		{
			System.out.println("\n Given k : " + k);
			// Print nodes at k distance
			printKthDistance(this.root, k);
		}
	}
	public static void main(String[] args)
	{
		// Create new binary trees 
		BinaryTree tree = new BinaryTree();
		/*
		             4                            
		           /   \    
		         -4     7    
		         / \     \               
		        2   3     12
		       /   / \    / 
		      1   10  8  5
		     /    /       \
		    3    9         12 

		    -----------------
		    Construct Binary Tree

		*/
		tree.root = new TreeNode(4);
		tree.root.left = new TreeNode(-4);
		tree.root.left.right = new TreeNode(3);
		tree.root.left.right.left = new TreeNode(10);
		tree.root.left.right.left.left = new TreeNode(9);
		tree.root.left.right.right = new TreeNode(8);
		tree.root.left.left = new TreeNode(2);
		tree.root.left.left.left = new TreeNode(1);
		tree.root.left.left.left.left = new TreeNode(3);
		tree.root.right = new TreeNode(7);
		tree.root.right.right = new TreeNode(12);
		tree.root.right.right.left = new TreeNode(5);
		tree.root.right.right.left.right = new TreeNode(5);
		// Test cases
		tree.rootToKDistance(4);
		tree.rootToKDistance(2);
		tree.rootToKDistance(0);
	}
}

input

 Given k : 4
 3 9 5
 Given k : 2
 2 3 12
 Given k : 0
 4
// Include header file
#include <iostream>
using namespace std;

/*
    C++ Program
    Print nodes at k distance from root
*/

// Binary Tree node
class TreeNode
{
	public: int data;
	TreeNode *left;
	TreeNode *right;
	TreeNode(int data)
	{
		// Set node value
		this->data = data;
		this->left = NULL;
		this->right = NULL;
	}
};
class BinaryTree
{
	public: TreeNode *root;
	BinaryTree()
	{
		this->root = NULL;
	}
	// Recursively display the nodes which are exist k distance from root
	void printKthDistance(TreeNode *node, int k)
	{
		if (node == NULL)
		{
			return;
		}
		if (k == 0)
		{
			// print k distance node
			cout << " " << node->data;
		}
		else
		{
			// Visit left and right subtree
			this->printKthDistance(node->left, k - 1);
			this->printKthDistance(node->right, k - 1);
		}
	}
	// Handles the request to print every k distance node from root
	void rootToKDistance(int k)
	{
		if (this->root == NULL)
		{
			cout << "Empty Binary Tree \n";
		}
		else
		{
			if (k < 0)
			{
				cout << "\n Invalid distance k : " << k << endl;
			}
			else
			{
				cout << "\n Given k : " << k << endl;
				// Print nodes at k distance
				this->printKthDistance(this->root, k);
			}
		}
	}
};
int main()
{
	// Create new binary trees
	BinaryTree *tree = new BinaryTree();
	/*
	             4                            
	           /   \    
	         -4     7    
	         / \     \               
	        2   3     12
	       /   / \    / 
	      1   10  8  5
	     /    /       \
	    3    9         12 
	    -----------------
	    Construct Binary Tree
	*/
	tree->root = new TreeNode(4);
	tree->root->left = new TreeNode(-4);
	tree->root->left->right = new TreeNode(3);
	tree->root->left->right->left = new TreeNode(10);
	tree->root->left->right->left->left = new TreeNode(9);
	tree->root->left->right->right = new TreeNode(8);
	tree->root->left->left = new TreeNode(2);
	tree->root->left->left->left = new TreeNode(1);
	tree->root->left->left->left->left = new TreeNode(3);
	tree->root->right = new TreeNode(7);
	tree->root->right->right = new TreeNode(12);
	tree->root->right->right->left = new TreeNode(5);
	tree->root->right->right->left->right = new TreeNode(5);
	// Test cases
	tree->rootToKDistance(4);
	tree->rootToKDistance(2);
	tree->rootToKDistance(0);
	return 0;
}

input

 Given k : 4
 3 9 5
 Given k : 2
 2 3 12
 Given k : 0
 4
// Include namespace system
using System;
/*
    Csharp Program
    Print nodes at k distance from root
*/
// Binary Tree node
public class TreeNode
{
	public int data;
	public TreeNode left;
	public TreeNode right;
	public TreeNode(int data)
	{
		// Set node value
		this.data = data;
		this.left = null;
		this.right = null;
	}
}
public class BinaryTree
{
	public TreeNode root;
	public BinaryTree()
	{
		// Set initial tree root to null
		this.root = null;
	}
	// Recursively display the nodes which are exist k distance from root
	public void printKthDistance(TreeNode node, int k)
	{
		if (node == null)
		{
			return;
		}
		if (k == 0)
		{
			// print k distance node
			Console.Write(" " + node.data);
		}
		else
		{
			// Visit left and right subtree
			this.printKthDistance(node.left, k - 1);
			this.printKthDistance(node.right, k - 1);
		}
	}
	// Handles the request to print every k distance node from root
	public void rootToKDistance(int k)
	{
		if (this.root == null)
		{
			Console.Write("Empty Binary Tree \n");
		}
		else
		{
			if (k < 0)
			{
				Console.WriteLine("\n Invalid distance k : " + k);
			}
			else
			{
				Console.WriteLine("\n Given k : " + k);
				// Print nodes at k distance
				this.printKthDistance(this.root, k);
			}
		}
	}
	public static void Main(String[] args)
	{
		// Create new binary trees
		BinaryTree tree = new BinaryTree();
		/*
		             4                            
		           /   \    
		         -4     7    
		         / \     \               
		        2   3     12
		       /   / \    / 
		      1   10  8  5
		     /    /       \
		    3    9         12 
		    -----------------
		    Construct Binary Tree
		*/
		tree.root = new TreeNode(4);
		tree.root.left = new TreeNode(-4);
		tree.root.left.right = new TreeNode(3);
		tree.root.left.right.left = new TreeNode(10);
		tree.root.left.right.left.left = new TreeNode(9);
		tree.root.left.right.right = new TreeNode(8);
		tree.root.left.left = new TreeNode(2);
		tree.root.left.left.left = new TreeNode(1);
		tree.root.left.left.left.left = new TreeNode(3);
		tree.root.right = new TreeNode(7);
		tree.root.right.right = new TreeNode(12);
		tree.root.right.right.left = new TreeNode(5);
		tree.root.right.right.left.right = new TreeNode(5);
		// Test cases
		tree.rootToKDistance(4);
		tree.rootToKDistance(2);
		tree.rootToKDistance(0);
	}
}

input

 Given k : 4
 3 9 5
 Given k : 2
 2 3 12
 Given k : 0
 4
<?php
/*
    Php Program
    Print nodes at k distance from root
*/
// Binary Tree node
class TreeNode
{
	public $data;
	public $left;
	public $right;
	public	function __construct($data)
	{
		// Set node value
		$this->data = $data;
		$this->left = NULL;
		$this->right = NULL;
	}
}
class BinaryTree
{
	public $root;
	public	function __construct()
	{
		$this->root = NULL;
	}
	// Recursively display the nodes which are exist k distance from root
	public	function printKthDistance($node, $k)
	{
		if ($node == NULL)
		{
			return;
		}
		if ($k == 0)
		{
			// print k distance node
			echo(" ".$node->data);
		}
		else
		{
			// Visit left and right subtree
			$this->printKthDistance($node->left, $k - 1);
			$this->printKthDistance($node->right, $k - 1);
		}
	}
	// Handles the request to print every k distance node from root
	public	function rootToKDistance($k)
	{
		if ($this->root == NULL)
		{
			echo("Empty Binary Tree \n");
		}
		else
		{
			if ($k < 0)
			{
				echo("\n Invalid distance k : ".$k.
					"\n");
			}
			else
			{
				echo("\n Given k : ".$k.
					"\n");
				// Print nodes at k distance
				$this->printKthDistance($this->root, $k);
			}
		}
	}
}

function main()
{
	// Create new binary trees
	$tree = new BinaryTree();
	/*
	             4                            
	           /   \    
	         -4     7    
	         / \     \               
	        2   3     12
	       /   / \    / 
	      1   10  8  5
	     /    /       \
	    3    9         12 
	    -----------------
	    Construct Binary Tree
	*/
	$tree->root = new TreeNode(4);
	$tree->root->left = new TreeNode(-4);
	$tree->root->left->right = new TreeNode(3);
	$tree->root->left->right->left = new TreeNode(10);
	$tree->root->left->right->left->left = new TreeNode(9);
	$tree->root->left->right->right = new TreeNode(8);
	$tree->root->left->left = new TreeNode(2);
	$tree->root->left->left->left = new TreeNode(1);
	$tree->root->left->left->left->left = new TreeNode(3);
	$tree->root->right = new TreeNode(7);
	$tree->root->right->right = new TreeNode(12);
	$tree->root->right->right->left = new TreeNode(5);
	$tree->root->right->right->left->right = new TreeNode(5);
	// Test cases
	$tree->rootToKDistance(4);
	$tree->rootToKDistance(2);
	$tree->rootToKDistance(0);
}
main();

input

 Given k : 4
 3 9 5
 Given k : 2
 2 3 12
 Given k : 0
 4
/*
    Node JS Program
    Print nodes at k distance from root
*/
// Binary Tree node
class TreeNode
{
	constructor(data)
	{
		// Set node value
		this.data = data;
		this.left = null;
		this.right = null;
	}
}
class BinaryTree
{
	constructor()
	{
		this.root = null;
	}
	// Recursively display the nodes which are exist k distance from root
	printKthDistance(node, k)
	{
		if (node == null)
		{
			return;
		}
		if (k == 0)
		{
			// print k distance node
			process.stdout.write(" " + node.data);
		}
		else
		{
			// Visit left and right subtree
			this.printKthDistance(node.left, k - 1);
			this.printKthDistance(node.right, k - 1);
		}
	}
	// Handles the request to print every k distance node from root
	rootToKDistance(k)
	{
		if (this.root == null)
		{
			process.stdout.write("Empty Binary Tree \n");
		}
		else
		{
			if (k < 0)
			{
				console.log("\n Invalid distance k : " + k);
			}
			else
			{
				console.log("\n Given k : " + k);
				// Print nodes at k distance
				this.printKthDistance(this.root, k);
			}
		}
	}
}

function main()
{
	// Create new binary trees
	var tree = new BinaryTree();
	/*
	             4                            
	           /   \    
	         -4     7    
	         / \     \               
	        2   3     12
	       /   / \    / 
	      1   10  8  5
	     /    /       \
	    3    9         12 
	    -----------------
	    Construct Binary Tree
	*/
	tree.root = new TreeNode(4);
	tree.root.left = new TreeNode(-4);
	tree.root.left.right = new TreeNode(3);
	tree.root.left.right.left = new TreeNode(10);
	tree.root.left.right.left.left = new TreeNode(9);
	tree.root.left.right.right = new TreeNode(8);
	tree.root.left.left = new TreeNode(2);
	tree.root.left.left.left = new TreeNode(1);
	tree.root.left.left.left.left = new TreeNode(3);
	tree.root.right = new TreeNode(7);
	tree.root.right.right = new TreeNode(12);
	tree.root.right.right.left = new TreeNode(5);
	tree.root.right.right.left.right = new TreeNode(5);
	// Test cases
	tree.rootToKDistance(4);
	tree.rootToKDistance(2);
	tree.rootToKDistance(0);
}
main();

input

 Given k : 4
 3 9 5
 Given k : 2
 2 3 12
 Given k : 0
 4
#    Python 3 Program
#    Print nodes at k distance from root

#  Binary Tree node
class TreeNode :
	def __init__(self, data) :
		#  Set node value
		self.data = data
		self.left = None
		self.right = None
	

class BinaryTree :
	def __init__(self) :
		self.root = None
	
	#  Recursively display the nodes which are exist k distance from root
	def printKthDistance(self, node, k) :
		if (node == None) :
			return
		
		if (k == 0) :
			#  print k distance node
			print(" ", node.data, end = "")
		else :
			#  Visit left and right subtree
			self.printKthDistance(node.left, k - 1)
			self.printKthDistance(node.right, k - 1)
		
	
	#  Handles the request to print every k distance node from root
	def rootToKDistance(self, k) :
		if (self.root == None) :
			print("Empty Binary Tree ")
		else :
			if (k < 0) :
				print("\n Invalid distance k : ", k)
			else :
				print("\n Given k : ", k)
				#  Print nodes at k distance
				self.printKthDistance(self.root, k)
			
		
	

def main() :
	tree = BinaryTree()
	#             4                            
	#           /   \    
	#         -4     7    
	#         / \     \               
	#        2   3     12
	#       /   / \    / 
	#      1   10  8  5
	#     /    /       \
	#    3    9         12 
	#    -----------------
	#    Construct Binary Tree
	tree.root = TreeNode(4)
	tree.root.left = TreeNode(-4)
	tree.root.left.right = TreeNode(3)
	tree.root.left.right.left = TreeNode(10)
	tree.root.left.right.left.left = TreeNode(9)
	tree.root.left.right.right = TreeNode(8)
	tree.root.left.left = TreeNode(2)
	tree.root.left.left.left = TreeNode(1)
	tree.root.left.left.left.left = TreeNode(3)
	tree.root.right = TreeNode(7)
	tree.root.right.right = TreeNode(12)
	tree.root.right.right.left = TreeNode(5)
	tree.root.right.right.left.right = TreeNode(5)
	#  Test cases
	tree.rootToKDistance(4)
	tree.rootToKDistance(2)
	tree.rootToKDistance(0)

if __name__ == "__main__": main()

input

 Given k :  4
  3  9  5
 Given k :  2
  2  3  12
 Given k :  0
  4
#    Ruby Program
#    Print nodes at k distance from root

#  Binary Tree node
class TreeNode 
	# Define the accessor and reader of class TreeNode
	attr_reader :data, :left, :right
	attr_accessor :data, :left, :right
	def initialize(data) 
		#  Set node value
		self.data = data
		self.left = nil
		self.right = nil
	end

end

class BinaryTree 
	# Define the accessor and reader of class BinaryTree
	attr_reader :root
	attr_accessor :root
	def initialize() 
		self.root = nil
	end

	#  Recursively display the nodes which are exist k distance from root
	def printKthDistance(node, k) 
		if (node == nil) 
			return
		end

		if (k == 0) 
			#  print k distance node
			print(" ", node.data)
		else 
			#  Visit left and right subtree
			self.printKthDistance(node.left, k - 1)
			self.printKthDistance(node.right, k - 1)
		end

	end

	#  Handles the request to print every k distance node from root
	def rootToKDistance(k) 
		if (self.root == nil) 
			print("Empty Binary Tree \n")
		else 
			if (k < 0) 
				print("\n Invalid distance k : ", k, "\n")
			else 
				print("\n Given k : ", k, "\n")
				#  Print nodes at k distance
				self.printKthDistance(self.root, k)
			end

		end

	end

end

def main() 
	#  Create new binary trees
	tree = BinaryTree.new()
	#             4                            
	#           /   \    
	#         -4     7    
	#         / \     \               
	#        2   3     12
	#       /   / \    / 
	#      1   10  8  5
	#     /    /       \
	#    3    9         12 
	#    -----------------
	#    Construct Binary Tree
	tree.root = TreeNode.new(4)
	tree.root.left = TreeNode.new(-4)
	tree.root.left.right = TreeNode.new(3)
	tree.root.left.right.left = TreeNode.new(10)
	tree.root.left.right.left.left = TreeNode.new(9)
	tree.root.left.right.right = TreeNode.new(8)
	tree.root.left.left = TreeNode.new(2)
	tree.root.left.left.left = TreeNode.new(1)
	tree.root.left.left.left.left = TreeNode.new(3)
	tree.root.right = TreeNode.new(7)
	tree.root.right.right = TreeNode.new(12)
	tree.root.right.right.left = TreeNode.new(5)
	tree.root.right.right.left.right = TreeNode.new(5)
	#  Test cases
	tree.rootToKDistance(4)
	tree.rootToKDistance(2)
	tree.rootToKDistance(0)
end

main()

input

 Given k : 4
 3 9 5
 Given k : 2
 2 3 12
 Given k : 0
 4
/*
    Scala Program
    Print nodes at k distance from root
*/
// Binary Tree node
class TreeNode(var data: Int , var left: TreeNode , var right: TreeNode)
{
	def this(data: Int)
	{
		// Set node value
		this(data, null, null);
	}
}
class BinaryTree(var root: TreeNode)
{
	def this()
	{
		this(null);
	}
	// Recursively display the nodes which are exist k distance from root
	def printKthDistance(node: TreeNode, k: Int): Unit = {
		if (node == null)
		{
			return;
		}
		if (k == 0)
		{
			// print k distance node
			print(" " + node.data);
		}
		else
		{
			// Visit left and right subtree
			printKthDistance(node.left, k - 1);
			printKthDistance(node.right, k - 1);
		}
	}
	// Handles the request to print every k distance node from root
	def rootToKDistance(k: Int): Unit = {
		if (this.root == null)
		{
			print("Empty Binary Tree \n");
		}
		else
		{
			if (k < 0)
			{
				println("\n Invalid distance k : " + k);
			}
			else
			{
				println("\n Given k : " + k);
				// Print nodes at k distance
				printKthDistance(this.root, k);
			}
		}
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		// Create new binary trees
		var tree: BinaryTree = new BinaryTree();
		/*
		             4                            
		           /   \    
		         -4     7    
		         / \     \               
		        2   3     12
		       /   / \    / 
		      1   10  8  5
		     /    /       \
		    3    9         12 
		    -----------------
		    Construct Binary Tree
		*/
		tree.root = new TreeNode(4);
		tree.root.left = new TreeNode(-4);
		tree.root.left.right = new TreeNode(3);
		tree.root.left.right.left = new TreeNode(10);
		tree.root.left.right.left.left = new TreeNode(9);
		tree.root.left.right.right = new TreeNode(8);
		tree.root.left.left = new TreeNode(2);
		tree.root.left.left.left = new TreeNode(1);
		tree.root.left.left.left.left = new TreeNode(3);
		tree.root.right = new TreeNode(7);
		tree.root.right.right = new TreeNode(12);
		tree.root.right.right.left = new TreeNode(5);
		tree.root.right.right.left.right = new TreeNode(5);
		// Test cases
		tree.rootToKDistance(4);
		tree.rootToKDistance(2);
		tree.rootToKDistance(0);
	}
}

input

 Given k : 4
 3 9 5
 Given k : 2
 2 3 12
 Given k : 0
 4
/*
    Swift 4 Program
    Print nodes at k distance from root
*/
// Binary Tree node
class TreeNode
{
	var data: Int;
	var left: TreeNode? ;
	var right: TreeNode? ;
	init(_ data: Int)
	{
		// Set node value
		self.data = data;
		self.left = nil;
		self.right = nil;
	}
}
class BinaryTree
{
	var root: TreeNode? ;
	init()
	{
		self.root = nil;
	}
	// Recursively display the nodes which are exist k distance from root
	func printKthDistance(_ node: TreeNode? , _ k : Int)
	{
		if (node == nil)
		{
			return;
		}
		if (k == 0)
		{
			// print k distance node
			print(" ", node!.data, terminator: "");
		}
		else
		{
			// Visit left and right subtree
			self.printKthDistance(node!.left, k - 1);
			self.printKthDistance(node!.right, k - 1);
		}
	}
	// Handles the request to print every k distance node from root
	func rootToKDistance(_ k: Int)
	{
		if (self.root == nil)
		{
			print("Empty Binary Tree ");
		}
		else
		{
			if (k < 0)
			{
				print("\n Invalid distance k : ", k);
			}
			else
			{
				print("\n Given k : ", k);
				// Print nodes at k distance
				self.printKthDistance(self.root, k);
			}
		}
	}
}
func main()
{
	// Create new binary trees
	let tree: BinaryTree = BinaryTree();
	/*
	             4                            
	           /   \    
	         -4     7    
	         / \     \               
	        2   3     12
	       /   / \    / 
	      1   10  8  5
	     /    /       \
	    3    9         12 
	    -----------------
	    Construct Binary Tree
	*/
	tree.root = TreeNode(4);
	tree.root!.left = TreeNode(-4);
	tree.root!.left!.right = TreeNode(3);
	tree.root!.left!.right!.left = TreeNode(10);
	tree.root!.left!.right!.left!.left = TreeNode(9);
	tree.root!.left!.right!.right = TreeNode(8);
	tree.root!.left!.left = TreeNode(2);
	tree.root!.left!.left!.left = TreeNode(1);
	tree.root!.left!.left!.left!.left = TreeNode(3);
	tree.root!.right = TreeNode(7);
	tree.root!.right!.right = TreeNode(12);
	tree.root!.right!.right!.left = TreeNode(5);
	tree.root!.right!.right!.left!.right = TreeNode(5);
	// Test cases
	tree.rootToKDistance(4);
	tree.rootToKDistance(2);
	tree.rootToKDistance(0);
}
main();

input

 Given k :  4
  3  9  5
 Given k :  2
  2  3  12
 Given k :  0
  4
/*
    Kotlin Program
    Print nodes at k distance from root
*/
// Binary Tree node
class TreeNode
{
	var data: Int;
	var left: TreeNode ? ;
	var right: TreeNode ? ;
	constructor(data: Int)
	{
		// Set node value
		this.data = data;
		this.left = null;
		this.right = null;
	}
}
class BinaryTree
{
	var root: TreeNode ? ;
	constructor()
	{
		this.root = null;
	}
	// Recursively display the nodes which are exist k distance from root
	fun printKthDistance(node: TreeNode ? , k : Int): Unit
	{
		if (node == null)
		{
			return;
		}
		if (k == 0)
		{
			// print k distance node
			print(" " + node.data);
		}
		else
		{
			// Visit left and right subtree
			this.printKthDistance(node.left, k - 1);
			this.printKthDistance(node.right, k - 1);
		}
	}
	// Handles the request to print every k distance node from root
	fun rootToKDistance(k: Int): Unit
	{
		if (this.root == null)
		{
			print("Empty Binary Tree \n");
		}
		else
		{
			if (k < 0)
			{
				println("\n Invalid distance k : " + k);
			}
			else
			{
				println("\n Given k : " + k);
				// Print nodes at k distance
				this.printKthDistance(this.root, k);
			}
		}
	}
}
fun main(args: Array < String > ): Unit
{
	// Create new binary trees
	val tree: BinaryTree = BinaryTree();
	/*
	             4                            
	           /   \    
	         -4     7    
	         / \     \               
	        2   3     12
	       /   / \    / 
	      1   10  8  5
	     /    /       \
	    3    9         12 
	    -----------------
	    Construct Binary Tree
	*/
	tree.root = TreeNode(4);
	tree.root?.left = TreeNode(-4);
	tree.root?.left?.right = TreeNode(3);
	tree.root?.left?.right?.left = TreeNode(10);
	tree.root?.left?.right?.left?.left = TreeNode(9);
	tree.root?.left?.right?.right = TreeNode(8);
	tree.root?.left?.left = TreeNode(2);
	tree.root?.left?.left?.left = TreeNode(1);
	tree.root?.left?.left?.left?.left = TreeNode(3);
	tree.root?.right = TreeNode(7);
	tree.root?.right?.right = TreeNode(12);
	tree.root?.right?.right?.left = TreeNode(5);
	tree.root?.right?.right?.left?.right = TreeNode(5);
	// Test cases
	tree.rootToKDistance(4);
	tree.rootToKDistance(2);
	tree.rootToKDistance(0);
}

input

 Given k : 4
 3 9 5
 Given k : 2
 2 3 12
 Given k : 0
 4


Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment







© 2021, kalkicode.com, All rights reserved