⬅ Previous Topic
Inorder Traversal of a Binary Tree using RecursionNext Topic ⮕
Left View of a Binary Tree⬅ Previous Topic
Inorder Traversal of a Binary Tree using RecursionNext Topic ⮕
Left View of a Binary Treeclass TreeNode:
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right
def inorderTraversal(root):
result = []
stack = []
current = root
while current or stack:
while current:
stack.append(current)
current = current.left
current = stack.pop()
result.append(current.val)
current = current.right
return result
if __name__ == '__main__':
# Construct binary tree:
# 1
# / \
# 2 3
# / \
# 4 5
root = TreeNode(1, TreeNode(2, TreeNode(4), TreeNode(5)), TreeNode(3))
print(inorderTraversal(root))
⬅ Previous Topic
Inorder Traversal of a Binary Tree using RecursionNext Topic ⮕
Left View of a Binary TreeYou can support this website with a contribution of your choice.
When making a contribution, mention your name, and programguru.org in the message. Your name shall be displayed in the sponsors list.