
- Python 3 Basic Tutorial
- Python 3 - Home
- What is New in Python 3
- Python 3 - Overview
- Python 3 - Environment Setup
- Python 3 - Basic Syntax
- Python 3 - Variable Types
- Python 3 - Basic Operators
- Python 3 - Decision Making
- Python 3 - Loops
- Python 3 - Numbers
- Python 3 - Strings
- Python 3 - Lists
- Python 3 - Tuples
- Python 3 - Dictionary
- Python 3 - Date & Time
- Python 3 - Functions
- Python 3 - Modules
- Python 3 - Files I/O
- Python 3 - Exceptions
Program to perform an Inorder Traversal of a binary tree in Python
Suppose we have a binary tree; we have to find a list that contains the inorder traversal of root as a list. As we know the inorder traversal is a way of traversing all nodes in a tree where we −
Recursively traverse the left subtree.
Traverse the current node.
Recursively traverse the right subtree.
We have to try to solve this problem in iterative fashion.
So, if the input is like
then the output will be [12,13,4,16,7,14,22]
To solve this, we will follow these steps −
inorder := a new list
stack := an empty stack
Do the following infinitely, do
if root is not null, then
push root into the stack
root := left of root
otherwise when stack is not empty, then
root := top element of stack and pop from stack
insert value of root at the end of inorder
root := right of root
otherwise,
come out from the loop
return inorder
Let us see the following implementation to get better understanding −
Example
class TreeNode: def __init__(self, value): self.val = value self.left = None self.right = None class Solution: def solve(self, root): inorder = [] stack = [] while True: if root: stack.append(root) root = root.left elif stack: root = stack.pop() inorder.append(root.val) root = root.right else: break return inorder ob = Solution() root = TreeNode(13) root.left = TreeNode(12) root.right = TreeNode(14) root.right.left = TreeNode(16) root.right.right = TreeNode(22) root.right.left.left = TreeNode(4) root.right.left.right = TreeNode(7) print(ob.solve(root))
Input
root = TreeNode(13) root.left = TreeNode(12) root.right = TreeNode(14) root.right.left = TreeNode(16) root.right.right = TreeNode(22) root.right.left.left = TreeNode(4) root.right.left.right = TreeNode(7)
Output
[12, 13, 4, 16, 7, 14, 22]
- Related Articles
- C++ Program to Perform Inorder Recursive Traversal of a Given Binary Tree
- C++ Program to Perform Inorder Non-Recursive Traversal of a Given Binary Tree
- Golang program to perform inorder tree traversal
- Binary Tree Inorder Traversal in Python
- Java Program to Perform the inorder tree traversal
- Inorder Traversal of a Threaded Binary Tree in C++
- Python Program to Build Binary Tree if Inorder or Postorder Traversal as Input
- Construct Binary Tree from Preorder and Inorder Traversal in Python
- Construct Binary Tree from Inorder and Postorder Traversal in Python
- Program to perform level order traversal of binary tree in C++
- Python Program to Find Nth Node in the Inorder Traversal of a Tree
- C++ Program to Perform Preorder Recursive Traversal of a Given Binary Tree
- C++ Program to Perform Postorder Recursive Traversal of a Given Binary Tree
- Program to generate tree using preorder and inorder traversal in python
- C++ Program to Perform Preorder Non-Recursive Traversal of a Given Binary Tree
