0% found this document useful (0 votes)
12 views2 pages

IBM - Round 3 - Test

ibm codes

Uploaded by

yivebow921
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views2 pages

IBM - Round 3 - Test

ibm codes

Uploaded by

yivebow921
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

**Given the root

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))

max_width = max(max_width, index - first_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

while queue and i < len(nodes):


current = queue.popleft()
if nodes[i] != 'null':
current.left = TreeNode(int(nodes[i]))
queue.append(current.left)
i += 1
if i < len(nodes) and nodes[i] != 'null':
current.right = TreeNode(int(nodes[i]))
queue.append(current.right)
i += 1

return root

# Read input
input_nodes = input().strip().split(',')
root = build_tree(input_nodes)
print(widthOfBinaryTree(root))

**Given two Strings


def longestCommonSubsequence(text1, text2):
m, n = len(text1), len(text2)
dp = [[0] * (n + 1) for _ in range(m + 1)]

for i in range(1, m + 1):


for j in range(1, n + 1):
if text1[i - 1] == text2[j - 1]:
dp[i][j] = dp[i - 1][j - 1] + 1
else:
dp[i][j] = max(dp[i - 1][j], dp[i][j - 1])

return dp[m][n]

# Read input
text1 = input().strip()
text2 = input().strip()

# Compute and print result


print(longestCommonSubsequence(text1, text2))

**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

You might also like