We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Tree: Level Order Traversal
Tree: Level Order Traversal
Sort by
recency
|
576 Discussions
|
Please Login in order to post a comment
class Node: def init(self, info): self.info = info
self.left = None
self.right = None self.level = None
class BinarySearchTree: def init(self): self.root = None
""" Node is defined as self.left (the left child of the node) self.right (the right child of the node) self.info (the value of the node) """ def levelOrder(root): if not root: return queue = [root] while queue: cur = queue.pop(0) print(cur.info, end=" ") if cur.left: queue.append(cur.left) if cur.right: queue.append(cur.right)
tree = BinarySearchTree() t = int(input())
arr = list(map(int, input().split()))
for i in range(t): tree.create(arr[i])
levelOrder(tree.root)
Better clarity in the comments public static void levelOrder(Node root) { // Use ArrayDeque as the queue for level order traversal Queue nodeQueue = new ArrayDeque<>();
My C code 😎😁