IBM - Round 3 - Test
IBM - Round 3 - Test
import collections
class TreeNode:
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right
def widthOfBinaryTree(root):
if not root:
return 0
max_width = 0
queue = collections.deque([(root, 0)])
while queue:
level_length = len(queue)
_, first_index = queue[0]
for _ in range(level_length):
node, index = queue.popleft()
if node.left:
queue.append((node.left, 2 * index))
if node.right:
queue.append((node.right, 2 * index + 1))
return max_width
def build_tree(nodes):
if not nodes or nodes[0] == 'null':
return None
root = TreeNode(int(nodes[0]))
queue = collections.deque([root])
i = 1
return root
# Read input
input_nodes = input().strip().split(',')
root = build_tree(input_nodes)
print(widthOfBinaryTree(root))
return dp[m][n]
# Read input
text1 = input().strip()
text2 = input().strip()
**BITS_
1)A book - do you do
2)Farcial - Serious
3)today -made
4)matches -No artical
5)year-in
6)Tread-Stride
7)spelling-embarrass
8)robbery-the police
9)error-c
10)early-if