Skip to content

Instantly share code, notes, and snippets.

@arpi-r
Created October 28, 2018 14:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arpi-r/1b1495450efef2fc41817ad06b3512ad to your computer and use it in GitHub Desktop.
Save arpi-r/1b1495450efef2fc41817ad06b3512ad to your computer and use it in GitHub Desktop.
codebuddy week7
# Definition for a binary tree node
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
# @param A : root node of tree
# @return a list of list of integers
def levelOrder(self, A):
r=[]
r1=[]
t=[]
if A==None:
return r
t.append(A.val)
r.append(t)
temp=[]
temp.append(A)
r1.append(temp)
i=0
while i<len(r):
l=r1[i]
t1=[]
t2=[]
j=0
while j<len(l):
if l[j].left!=None:
t1.append(l[j].left)
t2.append(l[j].left.val)
if l[j].right!=None:
t1.append(l[j].right)
t2.append(l[j].right.val)
j=j+1
if(len(t1)!=0):
r1.append(t1)
r.append(t2)
i=i+1
return r
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment