Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

python coding Problem 1: reconstructBT Given the preorder and inorder traversals of a tree, construct the binary tree, assuming the values of all nodes in

python coding

image text in transcribedimage text in transcribedimage text in transcribed
Problem 1: reconstructBT Given the preorder and inorder traversals of a tree, construct the binary tree, assuming the values of all nodes in the tree are distinct. The preorder and inorder traversals are represented as Python lists, with the elements being the values of the nodes. The output is the root of the constructed binary tree. Note: Assume the inputs are always valid, i.e., there is always a unique binary tree given the input preorder and inorder traversals. Example: Given preorder = [3, 9, 20, 15, 7] inorder = [9, 3, 15, 20, 7], the constructed binary tree is 3 9 20 15Problem 2: convertBSTtoGST Given the root of a binary search tree (BSD, convert it to a greater sum tree {CST} such that for each node in the HST, its value on! is updated to the sum of all values greater than or equal to mil. Note: 0 Assume thevalues ofall nodes are disnct. - You may change the values of the input BST directly or may create a new In'ee as the output. Either way, you need to return the root of the G511 Example: Given the root of the following EST: theGST is: Follow up: not as a requirement for the homework, but the problem as a DUI} solution (9: is the number of nodes in the input BSD. Can you gure it out? BinaryTreeNode class class BinaryTreeNode: def init_(self, data) : self . val = data # the data the node holds self . left = None # a pointer to the left child self . right = None 1. Reconstruct Binary Tree def reconstructBT (preorder, inorder ) : 2. Convert Binary Search Tree def convertBSTtoGST (root ) : 3. Top k Frequent Elements def kFrequent (nums, k) : 4. All paths from one vertex to another in DAG def allpaths (edges, source, destination)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Financial management theory and practice

Authors: Eugene F. Brigham and Michael C. Ehrhardt

12th Edition

978-0030243998, 30243998, 324422695, 978-0324422696

Students also viewed these Programming questions

Question

How does rotation differ from circumduction?

Answered: 1 week ago