Skip to content

Instantly share code, notes, and snippets.

@sreevidyavutukuru
Created July 22, 2017 19:41
Show Gist options
  • Save sreevidyavutukuru/a546df70c857f0eba73f75b9af35726a to your computer and use it in GitHub Desktop.
Save sreevidyavutukuru/a546df70c857f0eba73f75b9af35726a to your computer and use it in GitHub Desktop.
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution(object):
def rightSideView(self, root):
"""
:type root: TreeNode
:rtype: List[int]
"""
nodes = self.levelOrder(root,[[root]])
print map(lambda x: map(lambda n: n.val, x), nodes)
#for i,vals in enumerate(nodes):
#print map( , nodes)
#r = []
#for val in vals:
# r.append(val.val)
#print r
def levelOrder(self, root, nodes, i=0):
"""
:type root: TreeNode
:rtype: List[int]
"""
temp = []
if len(nodes[i]) != 0 or i==0:
j = i+1
for node in nodes[i]:
#print i
if node.left != None:
temp.append(node.left)
#print node.left.val
if node.right != None:
temp.append(node.right)
#print node.right.val
nodes.append(temp)
i += 1
self.levelOrder(root,nodes,i)
return nodes
else:
#print nodes
return nodes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment