Skip to content

Commit 1e5bdc7

Browse files
Update datastructures/trees/binary/tree/tree_utils.py
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
1 parent b88d25b commit 1e5bdc7

File tree

1 file changed

+6
-7
lines changed

1 file changed

+6
-7
lines changed

datastructures/trees/binary/tree/tree_utils.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from typing import List, Optional, Any
22
from datastructures.trees.binary.node import BinaryTreeNode
3-
from queue import Queue
3+
from collections import deque
44
from itertools import chain
55

66

@@ -12,31 +12,30 @@ def create_tree_from_nodes(nodes: List[Any]) -> Optional[BinaryTreeNode]:
1212
Returns:
1313
Optional[BinaryTreeNode]: The root of the created tree
1414
"""
15-
if len(nodes) == 0:
15+
if not nodes or nodes[0] is None:
1616
return None
1717

1818
root = BinaryTreeNode(nodes[0])
1919

20-
queue: Queue[BinaryTreeNode] = Queue()
21-
queue.put(root)
20+
queue = deque([root])
2221

2322
i = 1
2423
while i < len(nodes):
2524
# Get the next node from the queue
26-
curr = queue.get()
25+
curr = queue.popleft()
2726

2827
# If the node is not None, create a new TreeNode object for its left child,
2928
# set it as the left child of the current node, and add it to the queue
3029
if nodes[i] is not None:
3130
curr.left = BinaryTreeNode(nodes[i])
32-
queue.put(curr.left)
31+
queue.append(curr.left)
3332

3433
i += 1
3534
# If there are more nodes in the list and the next node is not None, create a new BinaryTreeNode for its
3635
# right child, set it as the right child of the current node, and add it to the queue
3736
if i < len(nodes) and nodes[i] is not None:
3837
curr.right = BinaryTreeNode(nodes[i])
39-
queue.put(curr.right)
38+
queue.append(curr.right)
4039

4140
i += 1
4241

0 commit comments

Comments
 (0)