Created
January 6, 2018 23:23
-
-
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.
This file contains hidden or 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
| 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