Iam_Sandeep

Boundary traversal of Binary tree

Mar 23rd, 2022
1,452
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.37 KB | None | 0 0
  1.  
  2. class Solution:
  3.     def printBoundaryView(self, root):
  4.         def l(root):
  5.             if not root:return []
  6.             ans,x=[],root
  7.             while x:
  8.                 if not x.left and not x.right:
  9.                     return list(ans)
  10.                 ans.append(x.data)
  11.                 if x.left:x=x.left
  12.                 else:x=x.right
  13.  
  14.         def r(root):
  15.             if not root:return []
  16.             ans,x=deque(),root
  17.             while x:
  18.                 if not x.left and not x.right:
  19.                     return list(ans)
  20.                 ans.appendleft(x.data)
  21.                 if x.right:x=x.right
  22.                 else:x=x.left
  23.             return ans
  24.         t=[]#for storing leaves not included root
  25.         def preorder(x):# only store leaves
  26.             if not x:return
  27.             if not x.left and not x.right and x!=root:t.append(x.data)
  28.             preorder(x.left)
  29.             preorder(x.right)
  30.        
  31.         preorder(root)
  32.         ans=[root.data]
  33.         if root.left:
  34.             ans+=l(root.left)
  35.         ans+=t
  36.         if root.right:
  37.             ans+=r(root.right)
  38.         return ans
  39.  
  40.  
  41. #{
  42. #  Driver Code Starts
  43. #Initial Template for Python 3
  44.  
  45. # function should return a list containing the boundary view of the binary tree
  46. #{
  47. #  Driver Code Starts
  48. import sys
  49.  
  50. import sys
  51. sys.setrecursionlimit(100000)
  52. #Contributed by Sudarshan Sharma
  53. from collections import deque
  54. # Tree Node
  55. class Node:
  56.     def __init__(self, val):
  57.         self.right = None
  58.         self.data = val
  59.         self.left = None
  60.  
  61. # Function to Build Tree  
  62. def buildTree(s):
  63.     #Corner Case
  64.     if(len(s)==0 or s[0]=="N"):          
  65.         return None
  66.        
  67.     # Creating list of strings from input
  68.     # string after spliting by space
  69.     ip=list(map(str,s.split()))
  70.    
  71.     # Create the root of the tree
  72.     root=Node(int(ip[0]))                    
  73.     size=0
  74.     q=deque()
  75.    
  76.     # Push the root to the queue
  77.     q.append(root)                            
  78.     size=size+1
  79.    
  80.     # Starting from the second element
  81.     i=1                                      
  82.     while(size>0 and i<len(ip)):
  83.         # Get and remove the front of the queue
  84.         currNode=q[0]
  85.         q.popleft()
  86.         size=size-1
  87.        
  88.         # Get the current node's value from the string
  89.         currVal=ip[i]
  90.        
  91.         # If the left child is not null
  92.         if(currVal!="N"):
  93.            
  94.             # Create the left child for the current node
  95.             currNode.left=Node(int(currVal))
  96.            
  97.             # Push it to the queue
  98.             q.append(currNode.left)
  99.             size=size+1
  100.         # For the right child
  101.         i=i+1
  102.         if(i>=len(ip)):
  103.             break
  104.         currVal=ip[i]
  105.        
  106.         # If the right child is not null
  107.         if(currVal!="N"):
  108.            
  109.             # Create the right child for the current node
  110.             currNode.right=Node(int(currVal))
  111.            
  112.             # Push it to the queue
  113.             q.append(currNode.right)
  114.             size=size+1
  115.         i=i+1
  116.     return root
  117.    
  118.    
  119. if __name__=="__main__":
  120.     t=int(input())
  121.     for _ in range(0,t):
  122.         s=input()
  123.         root=buildTree(s)
  124.         obj = Solution()
  125.         res = obj.printBoundaryView(root)
  126.         for i in res:
  127.             print (i, end = " ")
  128.         print('')
  129. # } Driver Code Ends
Advertisement
Add Comment
Please, Sign In to add comment