Created
March 28, 2020 23:32
-
-
Save DevMahmoud10/10e225c49bab7c9566eee97aa69bb45a to your computer and use it in GitHub Desktop.
my solution from month ago and now
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
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 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
problem link:
https://leetcode.com/problems/binary-tree-level-order-traversal-ii/