Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Consider the following problem which takes as input the root of a binary search tree, and calculates the sum of values in various nodes
Consider the following problem which takes as input the root of a binary search tree, and calculates the sum of values in various nodes (including double counting some nodes). static int pathSum (BSTNode root) { return helper (root); } static int helper (BSTNode root) { if (root ==null) { return 0; } int sum=root.value + helper (root.rightChild); if (root.value > 0) sum += helper (root.rightChild); } else sum + helper (root.leftChild); return sum + helper (root. leftChild); Write a recurrence relation for the worst case running time of the helper algorithm if the tree provided as input is a perfect binary tree. Solve the recurrence relation using the Master Theorem. Show your work. (8 points) Definition: A perfect binary tree is a tree in which all internal nodes have two children and all leaves have the same depth. For the purposes of this problem assume that if the size of the tree is n, the left and right subtrees are of size n/2.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
To write a recurrence relation for the worstcase running time of the helper algorithm when the input ...Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started