Created
July 22, 2017 19:41
-
-
Save sreevidyavutukuru/a546df70c857f0eba73f75b9af35726a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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