878 · Boundary of Binary Tree

Input: {1,#,2,3,4}
Output: [1,3,4,2]
Explanation:
1
\
2
/ \
3 4
The root doesn't have left subtree, so the root itself is left boundary.
The leaves are node 3 and 4.
The right boundary are node 1,2,4. Note the anti-clockwise direction means you should output reversed right boundary.
So order them in anti-clockwise without duplicates and we have [1,3,4,2].
Input: {1,2,3,4,5,6,#,#,#,7,8,9,10}
Output: [1,2,4,7,8,9,10,6,3]
Explanation:
1
/ \
2 3
/ \ /
4 5 6
/ \ / \
7 8 9 10
The left boundary are node 1,2,4. (4 is the left-most node according to definition)
The leaves are node 4,7,8,9,10.
The right boundary are node 1,3,6,10. (10 is the right-most node).
So order them in anti-clockwise without duplicate nodes we have [1,2,4,7,8,9,10,6,3].
class Solution:
def left_func(self,left,root):
temp=root
while(temp):

left.append(temp.val)
if temp.left == None and temp.right ==None:
temp=temp.left
left.pop(-1)
elif temp.left or temp==self.start:
temp=temp.left
else:
temp=temp.right
def preorder(self,leaf,temp):
if temp==None:
return
if temp.left == None and temp.right ==None:
leaf.append(temp.val)
self.preorder(leaf,temp.left)
self.preorder(leaf,temp.right)
def right_func(self,right,root):
temp=root
while(temp):

right.append(temp.val)
if temp.left == None and temp.right ==None:
temp=temp.right
right.pop(-1)
elif temp.right or temp==self.start:
temp=temp.right
else:
temp=temp.left
def boundary_of_binary_tree(self, root):
self.left_arr=[]
self.leafs=[]
self.right_arr=[]
self.start = root
arr=[]


self.left_func(self.left_arr , root)
self.preorder(self.leafs,root)
self.right_func(self.right_arr,root)
arr.extend(self.left_arr)
arr.extend(self.leafs)
self.right_arr=self.right_arr[::-1]
if self.right_arr:
self.right_arr.pop(-1)
arr.extend(self.right_arr)


return arr
class Solution:
def left_func(self,res,root):
temp=root.left
while(temp):


if temp.left != None or temp.right !=None:
res.append(temp.val)
if temp.left:
temp=temp.left
else:
temp=temp.right
def preorder(self,res,temp):

if temp.left == None and temp.right ==None:
res.append(temp.val)
return
if temp.left :
self.preorder(res,temp.left)
if temp.right:
self.preorder(res,temp.right)
def right_func(self,res,root):
temp=root.right
temporary=[]
while(temp):


if temp.left != None or temp.right !=None:
temporary.append(temp.val)
if temp.right:
temp=temp.right
else:
temp=temp.left
for i in range(len(temporary)-1,-1,-1):
res.append(temporary[i])
def boundary_of_binary_tree(self, root):
arr=[]
if root:
arr.append(root.val)
self.left_func(arr , root)
self.preorder(arr,root)
self.right_func(arr,root)
return arr

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Dhanarajappu

Dhanarajappu

Tech-Enthusiast, Coder,Explorer,Geeky,Software Engineer |A piece of code delivers everything that you need. The world is all about codes.