
- 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 find out if a linked list is present in a given binary tree in Python
Suppose we are given a binary tree that has a root node 'root' and a linked list that has a head node 'head'. We have to find out if that linked list exists in that binary tree. If a set of nodes in the tree have links with each other in order as a linked list, and if that order is similar to that of the provided linked list, then we return 'True' or otherwise, we return 'False'.
So, if the input is like
Tree
Linked List
then the output will be True.
To solve this, we will follow these steps −
- arr := a new list
- size := size of arr
- temp_arr := an array of size (size + 1) initialized with -1
- Define a function helper() . This will take root, val
- if val >= size, then
- return True
- if root is similar to None, then
- return False
- val := val + 1
- while val > 0 and value of root is not same as arr[val - 1], do
- val := temp_arr[val - 1] + 1
- if helper(left of root, val) or helper(right of root, val) is True, then
- return True
- return False
- if val >= size, then
- start := head
- while start is not same as null, do
- add the value of start at the end of arr
- start := next of start
- for node in range 1 to size + 1, do
- temp_arr[node] := temp_arr[node - 1] + 1
- while temp_arr[node] > 0 and arr[node - 1] is not same as arr[temp_arr[node] - 1], do
- temp_arr[node] := temp_arr[temp_arr[node] - 1] + 1
- return helper(root, 0)
Example
Let us see the following implementation to get better understanding −
class TreeNode: def __init__(self, val, left=None, right=None): self.val = val self.left = left self.right = right class ListNode: def __init__(self, val, next=None): self.val = val self.next = next def insert(temp,data): que = [] que.append(temp) while (len(que)): temp = que[0] que.pop(0) if (not temp.left): if data is not None: temp.left = TreeNode(data) else: temp.left = TreeNode(0) break else: que.append(temp.left) if (not temp.right): if data is not None: temp.right = TreeNode(data) else: temp.right = TreeNode(0) break else: que.append(temp.right) def make_tree(elements): node = TreeNode(elements[0]) for element in elements[1:]: insert(node, element) return node def make_list(elements): head = ListNode(elements[0]) for element in elements[1:]: ptr = head while ptr.next: ptr = ptr.next ptr.next = ListNode(element) return head def solve(root, head): arr = [] start = head while start: arr += (start.val,) start = start.next size = len(arr) temp_arr = [-1] * (size + 1) for node in range(1, size + 1): temp_arr[node] = temp_arr[node - 1] + 1 while temp_arr[node] > 0 and arr[node - 1] != arr[temp_arr[node] - 1]: temp_arr[node] = temp_arr[temp_arr[node] - 1] + 1 def helper(root, val): if val >= size: return True if not root: return False val += 1 while val > 0 and root.val != arr[val - 1]: val = temp_arr[val - 1] + 1 if helper(root.left, val) or helper(root.right, val): return True return False return helper(root, 0) root = make_tree([6, 7, 8, 9, 10]) head = make_list([6, 7, 10]) print(solve(root, head))
Input
root = make_tree([6, 7, 8, 9, 10]) head = make_list([6, 7, 10]) print(solve(root, head))
Output
True
- Related Articles
- Program to find out if a BST is present in a given binary tree in Python
- Python program to convert a given binary tree to doubly linked list
- Program to create linked list to binary search tree in Python
- Python Program to Implement Binary Tree using Linked List
- Program to convert linked list to zig-zag binary tree in Python
- Program to find out the largest sum value of a BST in a given binary tree in Python
- Program to convert a linked list into a binary search tree in C++
- Program to find folded list from a given linked list in Python
- Convert a given Binary Tree to Doubly Linked List (Set 1) in C++
- Convert a given Binary Tree to Doubly Linked List (Set 2) in C++
- Program to convert level order binary tree traversal to linked list in Python
- Program to convert binary search tree to a singly linked list in C++?
- Program to find out distance between two nodes in a binary tree in Python
- Check if a given Binary Tree is Heap in Python
- Linked List in Binary Tree in C++

Advertisements