# Flatten binary tree in order of postorder traversal in kotlin Kotlin program for Flatten binary tree in order of postorder traversal. Here mentioned other language solution.

``````/*
Kotlin program for
Flatten binary tree in order of post-order traversal
*/
// Node of Binary Tree
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 ? ;
var back: TreeNode ? ;
constructor()
{
this.root = null;
this.back = null;
}
// Recursive function
// Display postorder view of binary tree
fun postOrder(node: TreeNode ? ): Unit
{
if (node != null)
{
this.postOrder(node.left);
this.postOrder(node.right);
// Print node value
print("  " + node.data);
}
}
// This are flattening tree nodes in postorder from
fun transform(node: TreeNode ? ): Unit
{
if (node != null)
{
// Recursive executing left and right subtree
this.transform(node.left);
this.transform(node.right);
if (this.back == null)
{
// This is first node of postorder traversal
// Get first node of transform tree
this.root = node;
}
else
{
// Next node
this.back?.right = node;
// We taking only one direction
this.back?.left = null;
}
this.back = node;
}
}
// This are handling the request of
// flatten tree nodes in post order from.
fun flattenNode(): Unit
{
if (this.root == null)
{
// When empty tree
return;
}
// Set back node
this.back = null;
// Perform flatten operation
this.transform(this.root);
if (this.back != null)
{
// Set last node of post order
this.back?.left = null;
this.back?.right = null;
}
this.back = null;
}
// Display flatten elements of tree
fun showElement(): Unit
{
if (this.root == null)
{
println("\n Empty Tree");
return;
}
println("\n Flatten Tree Node in Postorder : ");
var temp: TreeNode ? = this.root;
// Iterate tree elements
while (temp != null)
{
// Display node value
print("  " + temp.data);
// Visit to next node
temp = temp.right;
}
}
}
fun main(args: Array < String > ): Unit
{
// New tree
val tree: BinaryTree = BinaryTree();
/*
Construct Binary Tree
-----------------------
1
/ \
/   \
6     8
/ \   / \
2   3 7   5
/   /   \   \
9   4    -6   11
*/
tree.root = TreeNode(1);
tree.root?.left = TreeNode(6);
tree.root?.left?.left = TreeNode(2);
tree.root?.right = TreeNode(8);
tree.root?.right?.right = TreeNode(5);
tree.root?.right?.left = TreeNode(7);
tree.root?.right?.left?.right = TreeNode(-6);
tree.root?.left?.right = TreeNode(3);
tree.root?.left?.right?.left = TreeNode(4);
tree.root?.left?.left?.left = TreeNode(9);
tree.root?.right?.right?.right = TreeNode(11);
// Display tree elements
println("\n Postorder Nodes : ");
tree.postOrder(tree.root);
// Testing
tree.flattenNode();
// After transform
tree.showElement();
}``````

Output

`````` Postorder Nodes :
9  2  4  3  6  -6  7  11  5  8  1
Flatten Tree Node in Postorder :
9  2  4  3  6  -6  7  11  5  8  1``````

## 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.