Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In order to balance a tree, we have to first find nodes about which rotations have to be made. In this question, complete the findLastUnbalanced

In order to balance a tree, we have to first find nodes about which rotations have to be made. In this question, complete the findLastUnbalanced function that accepts a TreeNode * root and finds the deepest node that is unbalanced. Remember that an unbalanced node has subtrees of heights differing by more than 1.

Note: If there are multiple unbalanced nodes, you have to return the one farthest from the root. If there are no unbalanaced nodes, return NULL.

Hint: You can use a helper function for calculating height.

GIVEN h file

#pragma once

#include

// Definition for a binary tree node. struct TreeNode { int val_; int balance_; TreeNode *left_; TreeNode *right_; TreeNode(int x) { left_ = NULL; right_ = NULL; val_ = x; balance_ = 0; } };

TreeNode* findLastUnbalanced(TreeNode* root);

void deleteTree(TreeNode* root);

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Design And SQL For DB2

Authors: James Cooper

1st Edition

1583473572, 978-1583473573

More Books

Students also viewed these Databases questions

Question

Calculate the following specific volumes

Answered: 1 week ago