sahnisol2-3

# sahnisol2-3 - Data Structures Algorithms Exam 2 Sample 3 75...

This preview shows pages 1–2. Sign up to view the full content.

Data Structures & Algorithms Exam 2 Sample 3, 75 Minutes Solutions 1. (a) We will use a static variable maxDifferenceSoFar to represent the maximum height difference detected so far. private static int maxDifferenceSoFar; Whenever we determine the height difference between the left and right subtrees of a new node, the absolute value of this difference is compared to maxDifferenceSoFar , and maxDifferenceSoFar updated if necessary. To implement a recursive strategy, we need to define a public and a private maxHeightDifference method. The public method is given below: public int maxHeightDifference() { maxDifferenceSoFar = 0; // initialize static variable maxHeightDifference(root); // compute max height difference return maxDifferenceSoFar; } The private method that does the actual computation of the maximum height difference is given below. This method also returns the height of the tree whose root is t . private static int maxHeightDifference(MyBinaryTreeNode t)

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

### Page1 / 4

sahnisol2-3 - Data Structures Algorithms Exam 2 Sample 3 75...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online