Grokking the Coding Interview: Patterns for Coding Questions
Ask Author
Back to course home

0% completed

Vote For New Content
Are we returnning anything here?

greenwald.juj

Sep 26, 2023

All the previous Tree BFS Level Order Examples are returning something, why aren't we doing it here? Is it inherent that the tree is being returned?

Also why does this one include the print function but previous versions don't? This makes it so you don't see the input and output when the Solution is Executed.

from __future__ import print_function from collections import deque class TreeNode: def __init__(self, val): self.val = val self.left, self.right, self.next = None, None, None # level order traversal using 'next' pointer def print_level_order(self): nextLevelRoot = self while nextLevelRoot: current = nextLevelRoot nextLevelRoot = None while current: print(str(current.val) + " ", end='') if not nextLevelRoot: if current.left: nextLevelRoot = current.left elif current.right: nextLevelRoot = current.right current = current.next print() def connect_level_order_siblings(root): if root is None: return queue = deque() queue.append(root) while queue: previousNode = None levelSize = len(queue) # connect all nodes of this level for _ in range(levelSize): currentNode = queue.popleft() if previousNode: previousNode.next = currentNode previousNode = currentNode # insert the children of current node in the queue if currentNode.left: queue.append(currentNode.left) if currentNode.right: queue.append(currentNode.right) def main(): root = TreeNode(12) root.left = TreeNode(7) root.right = TreeNode(1) root.left.left = TreeNode(9) root.right.left = TreeNode(10) root.right.right = TreeNode(5) connect_level_order_siblings(root) print("Level order traversal using 'next' pointer: ") root.print_level_order() main()

2

0

Comments
Comments

On this page