0% found this document useful (0 votes)
2 views

Assignment 1

The document contains two coding assignments related to data structures. The first assignment involves flattening a binary tree into a linked list using a pre-order traversal method, while the second assignment calculates the amount of water that can be trapped in an elevation map. Both assignments include Python code implementations and input/output instructions.

Uploaded by

t7zkwz7vh2
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Assignment 1

The document contains two coding assignments related to data structures. The first assignment involves flattening a binary tree into a linked list using a pre-order traversal method, while the second assignment calculates the amount of water that can be trapped in an elevation map. Both assignments include Python code implementations and input/output instructions.

Uploaded by

t7zkwz7vh2
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Advanced Coding- II

Assignment-1

Shreya sharon
VU21CSEN0101733
1) Given the root of a binary tree, flatten the tree into a "linked list":
The "linked list" should use the same TreeNode class where the right child pointer
points to the next node in the list and the left child pointer is always null.
The "linked list" should be in the same order as a pre-order traversal of the binary tree.

Code:-
class TreeNode:
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right

def flatten(root):
if not root:
return

# Flatten left and right subtrees recursively


flatten(root.left)
flatten(root.right)

# Store the right subtree


temp_right = root.right

# Move the left subtree to the right


root.right = root.left
root.left = None

# Traverse to the end of the new right subtree


current = root
while current.right:
current = current.right

# Attach the stored right subtree


current.right = temp_right

def build_tree(values):
if not values:
return None

from collections import deque


nodes = deque()
root = TreeNode(values[0])
nodes.append(root)
i=1

while i < len(values):


current = nodes.popleft()
if values[i] is not None:
current.left = TreeNode(values[i])
nodes.append(current.left)
i += 1
if i < len(values) and values[i] is not None:
current.right = TreeNode(values[i])
nodes.append(current.right)
i += 1

return root

def print_flattened_tree(root):
current = root
while current:
print(current.val, end=" -> " if current.right else "\n")
current = current.right

import ast
values = ast.literal_eval(input("Enter tree nodes as a list: "))
root = build_tree(values)

flatten(root)

print("Flattened tree:")
print_flattened_tree(root)

Output:-
Q2) Given n non-negative integers representing an elevation map where the
width of each bar is 1, compute how much water it can trap after raining.

Code:-
def trap(height):
if not height:
return 0

left, right = 0, len(height) - 1


left_max, right_max = 0, 0
trapped_water = 0

while left < right:


if height[left] < height[right]:
if height[left] >= left_max:
left_max = height[left]
else:
trapped_water += left_max - height[left]
left += 1
else:
if height[right] >= right_max:
right_max = height[right]
else:
trapped_water += right_max - height[right]
right -= 1

return trapped_water

height = list(map(int, input("Enter the elevation heights separated by spaces: ").split()))


print("Trapped water:", trap(height))

output

You might also like