Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

ECS 36C – Introduction to Data Structures Homework 06

Important:
• The purpose of the homework is to practice trees. If you didn’t meet the requirements as stated in the
description, only half of the points will be given.
• For this homework, you are given a main file to use for tests, and tree.h file and a homework.h file. You
must create the corresponding .cpp file called homework for submission.
• You may upload as many times as you want before the deadline. Each time the autograder will let you know
how many of the test cases you have passed/failed. To prevent people from manipulating the autogader,
the content of only half of the test cases are visible.
• Please do not copy others’ answers. Autograder can detect similar code.

Problem 1
In class, we discussed the preorder traversal of a binary tree. The code in the slides is recursive. Rewrite the
preorder function such that:

• it returns the nodes’ values in ank array instead of printing them out, and
• it is iterative instead of recursive.

Problem 2
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

1
Problem 3
Given the root of a binary search tree (BST), convert it to a greater sum tree (GST) such that for each node in the
BST, its value val is updated to the sum of all values greater than or equal to val.
Note:
• Assume the values of all nodes are distinct.
• You may change the values of the input BST directly or may create a new tree as the output. Either way,
you need to return the root of the GST.
Example:
Given the root of the following BST:

the GST is:

Follow up: not as a requirement for the homework, but the problem has a O(n) solution (n is the number of nodes
in the input BST). Can you figure it out?

Return the root node.

2
Problem 4

Given the root of a binary tree, determine if it is a valid binary search tree (BST). A valid BST is defined as
follows:

• The left subtree of a node contains only nodes with keys less than the node's key.
• The right subtree of a node contains only nodes with keys greater than the node's key.
• Both the left and right subtrees must also be binary search trees.
• This function should return True/False

Problem 5

Given the root of a binary tree, determine if it is an AVL tree. An AVL tree is a self-balancing binary search tree
where the difference in heights between the left and right subtrees of any node is at most one.

Requirements:

• Write a function that checks whether a given binary tree is an AVL tree.
• The function should return true if the tree is an AVL tree and false otherwise.

You might also like