-
Notifications
You must be signed in to change notification settings - Fork 0
/
boundary_traversal_binary_tree.py
55 lines (50 loc) · 1.28 KB
/
boundary_traversal_binary_tree.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#User function Template for python3
'''
class Node:
def __init__(self, val):
self.right = None
self.data = val
self.left = None
'''
# your task is to complete this function
# function should return a list containing the boundary view of the binary tree
def printBoundaryView(root):
# Code here
que = []
def inorder(root):
if root is None:
return
inorder(root.left)
if root.left is None and root.right is None:
que.append(root.data)
inorder(root.right)
que.append(root.data)
#print left
ptr = root.left
while ptr is not None:
if ptr.left is None :
if ptr.right is None:
break
else:
que.append(ptr.data)
ptr = ptr.right
else:
que.append(ptr.data)
ptr = ptr.left
#inorder
inorder(root.left)
inorder(root.right)
ptr = root.right
temp = []
while ptr is not None:
if ptr.right is None:
if ptr.left is None:
break
else:
temp.append(ptr.data)
ptr = ptr.left
else:
temp.append(ptr.data)
ptr = ptr.right
que = que+temp[::-1]
return que