# Flatten binary tree in order of postorder traversal in golang

Go program for Flatten binary tree in order of postorder traversal. Here more solutions.

``````package main
import "fmt"
/*
Go program for
Flatten binary tree in order of post-order traversal
*/
// Node of Binary Tree
type TreeNode struct {
data int
left * TreeNode
right * TreeNode
}
func getTreeNode(data int) * TreeNode {
// return new TreeNode
return &TreeNode {
data,
nil,
nil,
}
}
type BinaryTree struct {
root * TreeNode
back * TreeNode
}
func getBinaryTree() * BinaryTree {
// return new BinaryTree
return &BinaryTree {
nil,
nil,
}
}
// Recursive function
// Display postorder view of binary tree
func(this BinaryTree) postOrder(node * TreeNode) {
if node != nil {
this.postOrder(node.left)
this.postOrder(node.right)
// Print node value
fmt.Print("  ", node.data)
}
}
// This are flattening tree nodes in postorder from
func(this *BinaryTree) transform(node * TreeNode) {
if node != nil {
// Recursive executing left and right subtree
this.transform(node.left)
this.transform(node.right)
if this.back == nil {
// 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 = nil
}
this.back = node
}
}
// This are handling the request of
// flatten tree nodes in post order from.
func(this *BinaryTree) flattenNode() {
if this.root == nil {
// When empty tree
return
}
// Set back node
this.back = nil
// Perform flatten operation
this.transform(this.root)
if this.back != nil {
// Set last node of post order
this.back.left = nil
this.back.right = nil
}
this.back = nil
}
// Display flatten elements of tree
func(this BinaryTree) showElement() {
if this.root == nil {
fmt.Println("\n Empty Tree")
return
}
fmt.Println("\n Flatten Tree Node in Postorder : ")
var temp * TreeNode = this.root
// Iterate tree elements
for (temp != nil) {
// Display node value
fmt.Print("  ", temp.data)
// Visit to next node
temp = temp.right
}
}
func main() {
// New tree
var tree * BinaryTree = getBinaryTree()
/*
Construct Binary Tree
-----------------------
1
/ \
/   \
6     8
/ \   / \
2   3 7   5
/   /   \   \
9   4    -6   11
*/
tree.root = getTreeNode(1)
tree.root.left = getTreeNode(6)
tree.root.left.left = getTreeNode(2)
tree.root.right = getTreeNode(8)
tree.root.right.right = getTreeNode(5)
tree.root.right.left = getTreeNode(7)
tree.root.right.left.right = getTreeNode(-6)
tree.root.left.right = getTreeNode(3)
tree.root.left.right.left = getTreeNode(4)
tree.root.left.left.left = getTreeNode(9)
tree.root.right.right.right = getTreeNode(11)
// Display tree elements
fmt.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.