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

Week 5 Practice

This document contains summaries of three problems involving binary trees: 1. Deleting the root node from a binary heap removes the root and replaces it with the last node, bubbling it up or down to maintain the heap property. 2. Finding the sum of a binary search tree can be done recursively by returning the node value plus the sums of its left and right subtrees. 3. Finding the sum of values between lower and upper bounds in a binary search tree also uses recursion, returning the node value if it is in range, otherwise recursing left and right.

Uploaded by

Cüneyt Turan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views

Week 5 Practice

This document contains summaries of three problems involving binary trees: 1. Deleting the root node from a binary heap removes the root and replaces it with the last node, bubbling it up or down to maintain the heap property. 2. Finding the sum of a binary search tree can be done recursively by returning the node value plus the sums of its left and right subtrees. 3. Finding the sum of values between lower and upper bounds in a binary search tree also uses recursion, returning the node value if it is in range, otherwise recursing left and right.

Uploaded by

Cüneyt Turan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 16

Trees, Heaps, Priority Queues,

Tries, pt. 2
Practice Session
Azamat Ordabekov
Delete a node from Binary Heap
Given a Binary Heap (max heap). What happens if we delete a tree
root? Explain step by step
Delete a node from Binary Heap
Problem Set: Binary Search Tree Sum
Given Binary Search Tree. Write a function, which returns the sum of the
tree.
Example:
Input: Output: 128
15
/ \
7 35
/ \ /\
1 10 20 40
Problem Set: Binary Search Tree Sum
This problem can be solved by using recursion.
Tree Structure:

Given function input:


int sum(TreeNode node)
What is it recurrence relation?
What cases can have this function?
Problem Set: Binary Search Tree Sum
This problem can be solved by using recursion.
Given function input:
int sum(TreeNode node)
What is it recurrence relation:
node.value + sum(node.left) + sum(node.right)
What cases can have this function?
1. If node equals to NULL, then return 0
2. In all other cases return recurrence relation
Problem Set: Binary Search Tree Sum
Problem Set: BST Range Sum
Given a binary search tree, return the sum of values of all nodes with
value between L and R (inclusive).
Example:
Input: L = 9, R = 17Output: 25
15
/ \
7 35
/ \ /\
1 10 20 40
Problem Set: BST Range Sum
This problem can be solved by using recursion.
Tree Structure:

Given function input:


int sum(TreeNode node, int L, int R)
What is it recurrence relation?
What cases can have this function?
Problem Set: BST Range Sum
This problem can be solved by using recursion.
Given function input:
int sum(TreeNode node, int L, int R)
What is it recurrence relation:
node.value + sum(node.left, L, R) + sum(node.right, L, R)
What cases can have this function?
1. If node equals to NULL, then return 0
2. If the value of node is in the range, then return next recurrence relation:
node.value + sum(node.left, L, R) + sum(node.right, L, R)
3. If the value not in the range, then return default recurrence relation:
sum(node.left, L, R) + sum(node.right, L, R)
Problem Set: BST Range Sum
Problem Set: Symmetric Tree
Given a binary tree, write a function which checks whether the tree is
symmetric or not
Example:
Input: Output: True
6
/\
5 5
/\/\
1 10 10 1
Problem Set: Symmetric Tree
Input: Output: False
20
/\
12 12
\ \
5 5
Problem Set: Symmetric Tree
This problem can be solved by using recursion.
Tree Structure:

Given two function inputs:


1. bool isSymmetric(TreeNode node)
2. Bool checker(TreeNode left, TreeNode right)
What are recurrence relations of each function?
What cases can have these functions?
Problem Set: Symmetric Tree
Given two function inputs:
1. bool isSymmetric(TreeNode node)
2. Bool checker(TreeNode left, TreeNode right)
What are recurrence relations of each function?
First function: node == null || checker(node.left, node.right)
Second function: checker(left.left, right.right) && checker(left.right,
right.left)
What cases can have these functions?
Problem Set: Symmetric Tree

You might also like