Inorder traversal of binary tree with recursion in python
Python program for Inorder traversal of binary tree with recursion. Here problem description and explanation.
# Python 3 Program for
# inorder tree traversal of a Binary Tree
# using recursion
# 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
# Display Inorder view of binary tree
def inorder(self, node) :
if (node != None) :
# Visit left subtree
self.inorder(node.left)
# Print node value
print(node.data, end = " ")
# Visit right subtree
self.inorder(node.right)
def main() :
# Create new tree
tree = BinaryTree()
# Make A Binary Tree
# ----------------
# 15
# / \
# 24 54
# / / \
# 35 62 13
# Add tree TreeNode
tree.root = TreeNode(15)
tree.root.left = TreeNode(24)
tree.root.right = TreeNode(54)
tree.root.right.right = TreeNode(13)
tree.root.right.left = TreeNode(62)
tree.root.left.left = TreeNode(35)
# Display Tree Node
tree.inorder(tree.root)
if __name__ == "__main__": main()
Output
35 24 15 62 54 13
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