Code coverage report for lib/algorithms/4-binaryTrees/isBalanced.js

Statements: 87.5% (14 / 16)      Branches: 75% (6 / 8)      Functions: 100% (2 / 2)      Lines: 100% (12 / 12)      Ignored: none     

All files » lib/algorithms/4-binaryTrees/ » isBalanced.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23    1 3   3   1 21   9 9   9 9   9   7       1
//4.1 Determine if a binary tree is balanced
 
var isBalanced = function(node) {
	var BAD_VALUE = -1;
 
	return getHeight( 0, node) !== BAD_VALUE;
 
	function getHeight(n, node) {
		if(node === null) return n;
 
		var leftHeight = getHeight(n+1, node.left);
		Iif(leftHeight === BAD_VALUE) return BAD_VALUE;
 
		var rightHeight = getHeight(n+1, node.right);
		Iif(rightHeight === BAD_VALUE) return BAD_VALUE;
 
		if (Math.abs(leftHeight - rightHeight) > 1) return BAD_VALUE;
 
		return Math.max(leftHeight, rightHeight);
	};
};
 
module.exports = isBalanced;