Question

1. Assume that a tree is represented by X, lef t(X), right(X) and possibly other satellite...

1. Assume that a tree is represented by X, lef t(X), right(X) and possibly other satellite data that we ignore. Write an algorithm that checks if for any X, if hL is the hight of lef t(X) and hR is the height of right(X), then the algorithm return ”correct” if and only if for every vertex |hL−hR| ≤ 1. Namely, no left tree of any vertex is higher by two or more than the right tree of the vertex, and vise-versa. Remember to analyze the running time

Homework Answers

Answer #1

Here is an algorithm for the given task. The algorithm works recursively by going over all the vertices in a top-down manner and checking for the condition that |hL - hR| <= 1

Maintain a global array heights[]

Algorithm checkHeight(X):

if(X == null) heights[X] = 0; return "correct"

if(checkHeight(left(X)) && checkHeight(right(X))):

hL = heights[left(X)]

hR = heights[right(X)]

if(|hL - hR| <= 1) return "correct";

return "false";

Running Time:

The algorithm just goes once over each vertex, and hence runs in O(n) time.

You can comment below the answer in case of any doubts and I will be happy to help.

Please give a thumbs up if the answer could be of help!

All the best!

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Please answer the following Case analysis questions 1-How is New Balance performing compared to its primary...
Please answer the following Case analysis questions 1-How is New Balance performing compared to its primary rivals? How will the acquisition of Reebok by Adidas impact the structure of the athletic shoe industry? Is this likely to be favorable or unfavorable for New Balance? 2- What issues does New Balance management need to address? 3-What recommendations would you make to New Balance Management? What does New Balance need to do to continue to be successful? Should management continue to invest...
Using the model proposed by Lafley and Charan, analyze how Apigee was able to drive innovation....
Using the model proposed by Lafley and Charan, analyze how Apigee was able to drive innovation. case:    W17400 APIGEE: PEOPLE MANAGEMENT PRACTICES AND THE CHALLENGE OF GROWTH Ranjeet Nambudiri, S. Ramnarayan, and Catherine Xavier wrote this case solely to provide material for class discussion. The authors do not intend to illustrate either effective or ineffective handling of a managerial situation. The authors may have disguised certain names and other identifying information to protect confidentiality. This publication may not be...
What tools could AA leaders have used to increase their awareness of internal and external issues?...
What tools could AA leaders have used to increase their awareness of internal and external issues? ???ALASKA AIRLINES: NAVIGATING CHANGE In the autumn of 2007, Alaska Airlines executives adjourned at the end of a long and stressful day in the midst of a multi-day strategic planning session. Most headed outside to relax, unwind and enjoy a bonfire on the shore of Semiahmoo Spit, outside the meeting venue in Blaine, a seaport town in northwest Washington state. Meanwhile, several members of...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT