Python program to print path from root to a given node in a binary tree














































Python program to print path from root to a given node in a binary tree



# Python program to print path from root to a given node in a binary tree

# to print path from root to a given node
# first we append a node in array ,if it lies in the path 
# and print the array at last


# creating a new node
class new_node(object):
def __init__(self,value):
self.value=value
self.left=None
self.right=None

class Root_To_Node(object):
def __init__(self,root):
self.root=root


# function to check if current node in traversal 
# lies in path from root to a given node
# if yes then add this node to path_array
def check_path(self,root,key,path_array):
#base case
if root==None:
return False

# append current node in path_array
# if it does not lie in path then we will remove it.
path_array.append(root.value)
if root.value==key:
return True

if (root.left!=None and self.check_path(root.left,key,path_array)) or (root.right!=None and self.check_path(root.right,key,path_array)):
return True

#  if given key does not present in left or right subtree rooted at current node
#  then delete current node from array
# and return false

path_array.pop()
return False

def Print_Path(self,key):
path_array=[]
check=self.check_path(self.root,key,path_array)
if check:
return path_array
else:
return -1
if __name__=="__main__":
root=new_node(5)
root.left=new_node(2)
root.right=new_node(12)
root.left.left=new_node(1)
root.left.right=new_node(3)
root.right.left=new_node(9)
root.right.right=new_node(21)
root.right.right.left=new_node(19)
root.right.right.right=new_node(25)
obj=Root_To_Node(root)
key=int(input())
x=obj.Print_Path(key)
if x==-1:
print("node is not present in given tree")
else:
print(*x)
''' since we are going at each node only ones
   so time complexity of above algorithm is O(n)
   where n= no of nodes in the tree'''

More Articles of Saurabh Sisodia:

Name Views Likes
Python program to find difference between sums of odd level and even level nodes of a binary tree 750 17
Python Imaging Library(ImagePath Module) 1114 28
Python program to reverse a path in binary search tree using queue 1051 19
Python program to find sum of all the numbers that are formed from root to leaf paths 724 11
Python Imaging Library(ImageFont Module) 1658 27
Python program to find sum of leaf nodes at minimum level 826 16
Python Program to find the closest leaf node to a given node 878 16
Python program to check if given sorted sub-sequence exists in binary search tree 670 11
Python program to find successor for a given key in binary search tree without recursion 740 13
Introduction Of Python Imaging Library (Pillow) 1083 25
Python program to find pairs with given sum such that pair elements lie in different binary search trees 766 22
Python program to find kth ancestor of a node in binary tree 786 20
Python Imaging Library(Image Module) 1519 20
Python program to find the closest leaf in a binary tree 936 22
Python program to find the maximum sum leaf to root path in a binary tree 999 27
Python program to print duplicate elements from the binary search tree 718 15
Python program to find sum of value in each level of binary tree 1108 16
Python program to find distance between two given keys of a binary tree 772 15
Python program to find if there is a triplet in a Balanced binary search tree that adds to zero 919 14
Python Imaging Library(ImageFilter Module) 926 21
Python program to print path from root to a given node in a binary tree 1634 23
Python program to print middle level of perfect binary tree without finding height 697 20
Python Imaging Library(ImageMath Module) 762 20
Python program to find longest consecutive sequence in binary tree 1121 27
Python program to get level of a node in binary tree using recursion 790 15
Python program to count pairs from two binary search trees whose sum is equal to a given value x 691 24
Python program to convert binary tree to binary search tree 719 16
Python Imaging Library(ImageFile Module) 683 11
Python program to print the path common to the two paths from the root to the two given nodes 803 20
Python program to find distance from root to given node in a binary tree 983 15
Python Imaging Library(ExifTags Module) 945 13
Python Imaging Library(ImageSequence Module) 864 25
Python program to check whether a binary tree is a full binary tree or not without using recursion 686 15
Python program to check if a binary tree has duplicate values 1372 24
Python program to find k-th smallest element in binary search tree 772 24
Python Imaging Library(ImageStat Module) 1328 29
Python Imaging Library(ImageEnhance Module) 888 12
Python program to find longest path with same values in a binary tree 943 14
Python program to check if a binary tree is sorted level-wise or not 702 14
Python Imaging Library(ImageColor Module) 791 17
Python Imaging Library(ImageDraw Module) 1500 22
Python Imaging Library(ImageTk Module) 1107 20
Python program to check if a binary tree is subtree of another binary tree 1023 14
Python program to print ancestors of a given node in binary tree 1055 22
Python program to find number of turns to reach from one node to other in binary tree 821 25

Comments