CareerCup 4.1
//============================================================================
// Name : CareerCup41.cpp
// Author : SK2
// Version :
// Copyright : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================
#include <stdlib.h>
#include <iostream>
using namespace std;
struct node {
int data;
node * left;
node * right;
};
int getHeight(node * n) {
if(n->left == NULL && n->right == NULL)
return 0;
return ::max(getHeight(n->left), getHeight(n->right)) + 1;
}
bool checkBalance(node * root) {
if(root == NULL) return true;
int leftHeight = getHeight(root->left);
int rightHeight = getHeight(root->right);
if(leftHeight - rightHeight > 1 || leftHeight - rightHeight < -1)
return false;
return (checkBalance(root->left) && checkBalance(root->right));
}
int checkHeight(node * n) {
if(n == NULL) return 0;
int leftHeight = checkHeight(n->left);
if(leftHeight == -1) return -1;
int rightHeight = checkHeight(n->right);
if(rightHeight == -1) return -1;
if(abs(leftHeight - rightHeight))
return -1;
else
return max(leftHeight, rightHeight) + 1;
}
int main() {
cout << "!!!Hello World!!!" << endl; // prints !!!Hello World!!!
return 0;
}
CareerCup 4.3
//============================================================================
// Name : CareerCup43.cpp
// Author : SK2
// Version :
// Copyright : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================
#include <iostream>
using namespace std;
struct node {
int data;
node * left;
node * right;
};
node * createBST(int array[], int left, int right) {
node * n;
if(left > right) return NULL;
else {
int mid = (left + right) / 2;
n = new node();
n->data = array[mid];
n->left = createBST(array, left, mid - 1);
n->right = createBST(array, mid + 1, right);
}
return n;
}
node * createMinimalBST(int array[], int len) {
return createBST(array, 0, len - 1);
}
int main() {
cout << "!!!Hello World!!!" << endl; // prints !!!Hello World!!!
return 0;
}