-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbfs_lc.py
More file actions
38 lines (35 loc) · 1.09 KB
/
Copy pathbfs_lc.py
File metadata and controls
38 lines (35 loc) · 1.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#https://leetcode.com/problems/binary-tree-level-order-traversal/
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def levelOrder(self, root: TreeNode) -> List[List[int]]:
q=[]
q.append([])
while len(q)!=0:
value=q.pop(0)
if value.left:
q.append(value.left)
if value.right:
q.append(value.right)
print(value.val)
return [] #20 9
class Solution:
def levelOrder(self, root: TreeNode) -> List[List[int]]:
q=[]
q.append(root)
while len(q)!=0:
# print("length of q is:",len(q))
size=len(q)#No of parent elements
for i in range(0, size):
value=q.pop(0)
if value.left:
q.append(value.left)
if value.right:
q.append(value.right)
print(value.val)
print("----")
return []