Skip to content

Instantly share code, notes, and snippets.

@sourabh2k15
Created January 6, 2018 23:23
Show Gist options
  • Select an option

  • Save sourabh2k15/669c73ea30814f00b0c8668a3e883387 to your computer and use it in GitHub Desktop.

Select an option

Save sourabh2k15/669c73ea30814f00b0c8668a3e883387 to your computer and use it in GitHub Desktop.
Shad Khan's suggestion to eliminate dummy node in level order traversal code using size of queue.
from collections import deque
class Solution(object):
def levelOrder(self, root):
q = deque()
if(root != None):
q.append(root)
levels = []
while(len(q) != 0):
level = []
lenq = len(q)
for i in range(lenq):
temp = q.popleft()
if(temp.left != None):
q.append(temp.left)
if(temp.right != None):
q.append(temp.right)
level.append(temp.val)
levels.append(level)
return levels
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment