week7‐week8(recursion) - Joanna-0427/leetcode GitHub Wiki

# Merging Missions

The Avengers are planning multiple missions, and each mission has a priority level represented as a node in a linked list. You are given the heads of two sorted linked lists, mission1 and mission2, where each node represents a mission with its priority level.

Implement a recursive function merge_missions() which merges these two mission lists into one sorted list, ensuring that the combined list maintains the correct order of priorities. The merged list should be made by splicing together the nodes from the first two lists.

Return the head of the merged mission linked list.

class Node: def init(self, value, next=None): self.value = value self.next = next

def print_linked_list(head): current = head while current: print(current.value, end=" -> " if current.next else "\n") current = current.next

def merge_missions(mission1, mission2): pass Example Usage:

mission1 = Node(1, Node(2, Node(4))) mission2 = Node(1, Node(3, Node(4)))

print_linked_list(merge_missions(mission1, mission2))

# Calculating Village Size

In the kingdom of Codepathia, the queen determines how many resources to distribute to a village based on its class. A village's class is equal to the number of digits in its population. Given an integer population, write a function get_village_class() that returns the number of digits in population. def get_village_class_recursive(population): pass

# Palindromic Name

Queen Ada is superstitious and believes her children will only have good fortune if their name is symmetrical and reads the same forward and backward. Write a recursive function that takes in a string comprised of only lowercase alphabetic characters name and returns True if the name is palindromic and False otherwise.

Evaluate the time complexity of your solution. Define your variables and provide a rationale for why you believe your solution has the stated time complexity.

def is_palindrome(name): pass Example Usage:

print(is_palindrome("eve")) print(is_palindrome("ling")) print(is_palindrome(""))

# Adding a New Plant to the Collection

You have just purchased a new houseplant and are excited to add it to your collection! Your collection is meticulously organized using a Binary Search Tree (BST) where each node in the tree represents a houseplant in your collection, and houseplants are organized alphabetically by name (val).

Given the root of your BST collection and a new houseplant name, insert a new node with value name into your collection. Return the root of your updated collection. If another plant with name already exists in the tree, add the new node in the existing node's right subtree.

class TreeNode: def init(self, value, left=None, right=None): self.val = value self.left = left self.right = right

def add_plant(collection, name): pass values = ["Money Tree", "Fiddle Leaf Fig", "Snake Plant"] collection = build_tree(values)