We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 47975cf commit bc1c0feCopy full SHA for bc1c0fe
Leetcode_30day_challenge/Binary_Tree_Level_Order_Traversal.py
@@ -0,0 +1,27 @@
1
+class Solution:
2
+ def levelOrder(self, root: Optional[TreeNode]) -> List[List[int]]:
3
+ if not root:
4
+ return None
5
+ q = [root]
6
+ sep = '$'
7
+ ans = []
8
+ q.append(sep)
9
+ res = []
10
+ prev = -1
11
+ while q:
12
+ temp = q.pop(0)
13
+ if prev == temp:
14
+ break
15
+ if temp != sep:
16
+ res.append(temp.val)
17
+ if temp.left:
18
+ q.append(temp.left)
19
+ if temp.right:
20
+ q.append(temp.right)
21
+ else:
22
+ ans.append(list(res))
23
24
25
+ prev = temp
26
+
27
+ return ans
0 commit comments