Programming Fundamentals Quiz No 02

Here is the Programming Fundamentals Quiz No 02

Question:

Find the balance factor of the node of a BST and determine weather the BST is balance or Not.

Code:

Here is the Programming Fundamentals Quiz No 02

#include<iosream>

struct TreeNode
{
int val;
TreeNode* left;
TreeNode* right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};

int getHeight(TreeNode* root)
{
if (root == NULL) {
return 0;
}
return std::max(getHeight(root->left), getHeight(root->right)) + 1;
}

bool isBalanced(TreeNode* root)
{
if (root == NULL) {
return true;
}
int heightDifference = std::abs(getHeight(root->left) - getHeight(root->right));
if (heightDifference > 1)
{
return false;
} else {
return isBalanced(root->left) && isBalanced(root->right);
}
}

For more details about Huffman coding click here

For other assignments and quizzes click here 

Output:

Leave a Reply

Your email address will not be published. Required fields are marked *