File tree Expand file tree Collapse file tree 1 file changed +6
-7
lines changed
datastructures/trees/binary/tree Expand file tree Collapse file tree 1 file changed +6
-7
lines changed Original file line number Diff line number Diff line change 11from typing import List , Optional , Any
22from datastructures .trees .binary .node import BinaryTreeNode
3- from queue import Queue
3+ from collections import deque
44from 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
You can’t perform that action at this time.
0 commit comments