COL106_WeeklyAssignments_Week5
COL106_WeeklyAssignments_Week5
January 2025
Instructions
• Please use the following questions as practice questions for learning about List datastructure.
• The questions with * next to them should be attempted during the lab sessions, and your solutions
must be uploaded on moodlenew. Note that your submissions will not be evaluated, but will be used as
a mark of your attendance. We will filter out all the submissions that are not from the lab workstations.
So do not use your laptops for submitting the programs.
Questions
A binary tree is a hierarchical data structure in which each node has at most two children, referred to as the
left child and right child. It is widely used in applications such as searching, sorting, and hierarchical data
representation. Special types of binary trees include Binary Search Trees (BSTs), where the left child contains
values smaller than the parent and the right child contains values greater than the parent, and Balanced
Trees, which maintain a height balance to ensure efficient operations. Here’s a simple implementation of a
binary tree Node in Java:
1 class Node {
2 int value ;
3 Node left , right ;
4
1
Input: preorder = [3,9,20,15,7], inorder = [9,3,15,20,7]
Output:
3
/ \
9 20
/ \
15 7
Examples
Example 1:
Explanation:
3
/ \
9 20
/ \
15 7
Example
Input:
2
Output:
[1, 3, 4]
From the right side, the visible nodes are [1, 3, 4].
4. *Validate Binary Tree Given the root of a binary tree, determine if it is a valid binary search tree
(BST). A valid binary search tree 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.
Input Format The input is given as a reference to the root node of a binary tree. Example
Explanation:
2
/ \
1 3
Example 2:
Explanation:
5
/ \
1 4
/ \
3 6
The root node’s right child (4) contains a value less than the root (5),
violating the BST property.
5. Merge BSTs You are given n Binary Search Tree (BST) root nodes for n separate BSTs stored in
an array trees (0-indexed). Each BST in trees has at most 3 nodes, and no two roots have the same
value.
In one operation, you can:
• Select two distinct indices i and j such that the value stored at one of the leaf nodes of trees[i]
is equal to the root value of trees[j].
3
• Replace the leaf node in trees[i] with trees[j].
• Remove trees[j] from trees.
Return the root of the resulting BST if it is possible to form a valid BST after performing n − 1
operations. If it is impossible to create a valid BST, return null.
Input Format
• trees: An array of TreeNode objects where each element represents the root of a BST.
– Each BST has at most 3 nodes.
– No two roots have the same value.
Output Format
Return the root node of the resulting BST if it is possible to combine all the given BSTs into a single
valid BST. Otherwise, return null.
Example
• input
• output
4
Output Format: Your function should return an integers, the k th smallest element of the BST. It is
guaranteed that a suitable k th smallest element will always exist.
Example 1:
Explanation:
5
/ \
3 6
/ \
2 4
/
1
Explanation:
3
/ \
1 4
\
2
5
Explanation:
1
/ \
2 3
Example 2:
Explanation:
-5
/ \
1 4
/ \
3 6
Follow Up Question: The diameter/width of a tree is defined as the number of edges on the longest
path between any two nodes.
Can you use the solution for the maximum path sum problem (with slight modification) to find the
length of diameter of any n-ary tree?
Input: parents = [-1, 0, 0, 1, 1, 2, 2], findAncestorArray = [[3, 1], [5, 2], [6, 3]]
Output: [1,0,-1]
Explanation:
0
6
/ \
1 2
/ \ / \
3 4 5 6
The first ancestor of 3 is 1, second ancestor of 5 is 0 and third ancestor of 6 doesn’t exist.
9. Maximum distance for each node:
You are given a tree consisting of n nodes. Your task is to determine for each node the maximum
distance to any another node.
Input Format:
• parent[] (an array of integers): parent[i] is the parent of the ith node. The root of the tree is
node 0
Output Format: Your function should return an array of integers, where the ith element represents
the maximum distance from ith node to any other node.
Example 1:
Explanation:
0
/ \
1 2
/ \
3 4
Farthest from 0 are 3 or 4, from 1 are 3 or 4, from 2 is 1, and for 3 and 4 is 1. Therefore, we get
farthest distances as 2,3,2,3 and 3 respectively.