from typing import List from queue import Queue from TreeNode import * class BinaryTree: def __init__(self, nodes): self.root = self.createBinaryTree(nodes) def createBinaryTree(self, nodes): if len(nodes) == 0: return None # Create the root node of the binary tree root = TreeNode(nodes[0].data) # Create a queue and add the root node to it queue = Queue() queue.put(root) # Start iterating over the list of nodes starting from the second node i = 1 while i < len(nodes): # Get the next node from the queue curr = queue.get() # If the node is not None, create a new TreeNode object for its left child, # set it as the left child of the current node, and add it to the queue if nodes[i] is not None: curr.left = TreeNode(nodes[i].data) queue.put(curr.left) i += 1 # If there are more nodes in the list and the next node is not None, # create a new TreeNode object for its right child, set it as the right child # of the current node, and add it to the queue if i < len(nodes) and nodes[i] is not None: curr.right = TreeNode(nodes[i].data) queue.put(curr.right) i += 1 # Return the root of the binary tree return root