/* Wesley R Abbey
 *
 * Problem: Implement a function checks to see if a tree is balanced or not.
 */

'use strict';

var Node = function(data) {
	this.data = data;
	this.right = null;
	this.left = null;
};

function getHeight (node) {
	if (node === null) {
		return 0;
	};
	var leftHeight = getHeight(node.left);
	var rightHeight = getHeight(node.right);

	return Math.max(leftHeight, rightHeight) + 1;
}

function isBalanced (node) {
	// Base
	if (node === null) {
		return true;
	}

	var leftHeight  = getHeight(node.left);
	var rightHeight = getHeight(node.right);

	var heightDifference = Math.abs((leftHeight - rightHeight));

	if (heightDifference > 1) {
		return false;
	} else {
		return isBalanced(node.left) && isBalanced(node.right);
	}
}