# Flatten binary tree in order of postorder traversal in python

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

``````#  Python 3 program for
#  Flatten binary tree in order of post-order traversal

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

class BinaryTree :
def __init__(self) :
#  Set initial tree root to null
self.root = None
self.back = None
#  Recursive function
#  Display postorder view of binary tree
def postOrder(self, node) :
if (node != None) :
self.postOrder(node.left)
self.postOrder(node.right)
#  Print node value
print(" ",node.data, end ="")
#  This are flattening tree nodes in postorder from
def transform(self, node) :
if (node != None) :
#  Recursive executing left and right subtree
self.transform(node.left)
self.transform(node.right)
if (self.back == None) :
#  This is first node of postorder traversal
#  Get first node of transform tree
self.root = node
else :
#  Next node
self.back.right = node
#  We taking only one direction
self.back.left = None
self.back = node
#  This are handling the request of
#  flatten tree nodes in post order from.
def flattenNode(self) :
if (self.root == None) :
#  When empty tree
return
#  Set back node
self.back = None
#  Perform flatten operation
self.transform(self.root)
if (self.back != None) :
#  Set last node of post order
self.back.left = None
self.back.right = None
self.back = None
#  Display flatten elements of tree
def showElement(self) :
if (self.root == None) :
print("\n Empty Tree")
return
print("\n Flatten Tree Node in Postorder : ")
temp = self.root
#  Iterate tree elements
while (temp != None) :
#  Display node value
print(" ",temp.data, end ="")
#  Visit to next node
temp = temp.right
@staticmethod
def main() :
#  New tree
tree = 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
print("\n Postorder Nodes : ")
tree.postOrder(tree.root)
#  Testing
tree.flattenNode()
#  After transform
tree.showElement()

if __name__=="__main__":
BinaryTree.main()``````

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.