Skip to content

Commit af3aae9

Browse files
committed
add solution of 102
102. Binary Tree Level Order Traversal - https://leetcode.com/problems/binary-tree-level-order-tra
1 parent fc196f8 commit af3aae9

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+
"""
2+
102. Binary Tree Level Order Traversal
3+
- https://leetcode.com/problems/binary-tree-level-order-traversal/
4+
"""
5+
6+
# Method 1
7+
# Using List
8+
class Solution(object):
9+
def levelOrder(self, root):
10+
"""
11+
:type root: TreeNode
12+
:rtype: List[List[int]]
13+
"""
14+
result = []
15+
if not root:
16+
return result
17+
temp = [root]
18+
while temp:
19+
ans = []
20+
temp2 = []
21+
for i in range(len(temp)):
22+
ans.append(temp[i].val)
23+
if temp[i].left: temp2.append(temp[i].left)
24+
if temp[i].right: temp2.append(temp[i].right)
25+
result.append(ans)
26+
temp = temp2
27+
return result

0 commit comments

Comments
 (0)