Skip to content

Instantly share code, notes, and snippets.

@DevMahmoud10
Created March 28, 2020 23:32
Show Gist options
  • Save DevMahmoud10/10e225c49bab7c9566eee97aa69bb45a to your computer and use it in GitHub Desktop.
Save DevMahmoud10/10e225c49bab7c9566eee97aa69bb45a to your computer and use it in GitHub Desktop.
my solution from month ago and now
class Solution:
def levelOrderBottom(self, root: TreeNode) -> List[List[int]]:
if not root:return []
res=[]
q=[(root,0)]
while len(q)>0:
node,level=q.pop(0)
if len(res)<level+1:
res.append([])
res[level].append(node.val)
if node.left:
q.append((node.left,level+1))
if node.right:
q.append((node.right,level+1))
return res[::-1]
#old code
# if root==None:
# return []
# res=[[root.val]]
# level=[]
# q=[root]
# current_lvl_size=0
# l=1
# while len(q)>0:
# current=q.pop(0)
# if current:
# q.append(current.left)
# q.append(current.right)
# level.append(current.val)
# current_lvl_size+=1
# else:
# current_lvl_size+=2
# if current_lvl_size==2**l:
# res.insert(0,level)
# l+=1
# level=[]
# current_lvl_size=0
# return res
@DevMahmoud10
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment