
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Program to convert level order binary tree traversal to linked list in Python
Suppose we have a binary search tree, we have to convert it to a singly linked list using levelorder traversal.
So, if the input is like
then the output will be [5, 4, 10, 2, 7, 15, ]
To solve this, we will follow these steps −
head := a new linked list node
currNode := head
q := a list with value root
while q is not empty, do
curr := delete first element from q
if curr is not null, then
next of currNode := a new linked list node with value of curr
currNode := next of currNode
insert left of curr at the end of q
insert right curr at the end of q
return next of head
Let us see the following implementation to get better understanding −
Example
class ListNode: def __init__(self, data, next = None): self.val = data self.next = next class TreeNode: def __init__(self, data, left = None, right = None): self.val = data self.left = left self.right = right def print_list(head): ptr = head print('[', end = "") while ptr: print(ptr.val, end = ", ") ptr = ptr.next print(']') class Solution: def solve(self, root): head = ListNode(None) currNode = head q = [root] while q: curr = q.pop(0) if curr: currNode.next = ListNode(curr.val) currNode = currNode.next q.append(curr.left) q.append(curr.right) return head.next ob = Solution() root = TreeNode(5) root.left = TreeNode(4) root.right = TreeNode(10) root.left.left = TreeNode(2) root.right.left = TreeNode(7) root.right.right = TreeNode(15) head = ob.solve(root) print_list(head)
Input
root = TreeNode(5) root.left = TreeNode(4) root.right = TreeNode(10) root.left.left = TreeNode(2) root.right.left = TreeNode(7) root.right.right = TreeNode(15) head = ob.solve(root)
Output
[5, 4, 10, 2, 7, 15, ]
- Related Articles
- Binary Tree Zigzag Level Order Traversal in Python
- Program to perform level order traversal of binary tree in C++
- Binary Tree Level Order Traversal in C++
- Program to convert linked list to zig-zag binary tree in Python
- Python program to convert a given binary tree to doubly linked list
- Python Program to Implement Binary Tree using Linked List
- Program to create linked list to binary search tree in Python
- Program to convert binary search tree to a singly linked list in C++?
- Program to convert a linked list into a binary search tree in C++
- Level Order Tree Traversal in Data Structures
- C++ Program to Implement Double Order Traversal of a Binary Tree
- N-ary Tree Level Order Traversal in C++
- Binary Tree Vertical Order Traversal in C++
- Program to convert Linked list representing binary number to decimal integer in Python
- Flatten Binary Tree to Linked List in C++

Advertisements