# Print all odd nodes of Binary Search Tree in kotlin

Kotlin program for Print all odd nodes of Binary Search Tree. Here mentioned other language solution.

``````// Kotlin program for
// Detect and print all leaf nodes in binary search tree

// Binary Search Tree Node
class TreeNode
{
// Data value
var data: Int;
// Indicates left and right subtree
var left: TreeNode ? ;
var right: TreeNode ? ;
constructor(data: Int)
{
this.data = data;
this.left = null;
this.right = null;
}
}
class BinarySearchTree
{
var root: TreeNode ? ;
constructor()
{
this.root = null;
}
// Insert a node in BST
{
// Create new node of binary search tree
val node: TreeNode = TreeNode(value);
if (this.root == null)
{
// When adds a first node in binary tree
this.root = node;
}
else
{
var find: TreeNode ? = this.root;
// Add new node to proper position
while (find != null)
{
if (find.data >= value)
{
if (find.left == null)
{
find.left = node;
return;
}
else
{
// Visit left sub-tree
find = find.left;
}
}
else
{
if (find.right == null)
{
find.right = node;
return;
}
else
{
// Visit right sub-tree
find = find.right;
}
}
}
}
}
// Display the value of odd nodes in binary search tree
fun printOddNode(node: TreeNode ? ): Unit
{
if (node != null)
{
// Visit to left child
this.printOddNode(node.left);
if (node.data % 2 != 0)
{
// When node contain odd value
print("  " + node.data);
}
// Visit to right child
this.printOddNode(node.right);
}
}
}
fun main(args: Array < String > ): Unit
{
val tree: BinarySearchTree = BinarySearchTree();
// Add nodes in binary search tree
/*
5
/ \
/   \
/     \
3       9
/ \     / \
1   4   8   11
/ \     /      \
-3   2   6        12

*/
// Test
tree.printOddNode(tree.root);
}``````

Output

``  -3  1  3  5  9  11``

## Comment

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.