Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit bc1c0fe

Browse files
authored
Create Binary_Tree_Level_Order_Traversal.py
1 parent 47975cf commit bc1c0fe

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
res = []
24+
q.append(sep)
25+
prev = temp
26+
27+
return ans

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /