Given a binary tree, determine if it is height-balanced. A height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never differs by more than .
The tree is defined as follows:
// Java
class TreeNode
{
public:
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) {
val = x;
left = NULL;
right = NULL;
}
};// C++
class TreeNode
{
public:
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};Implement the function isBalanced that returns true if the tree is balanced and false otherwise.
// Java
boolean isBalanced(TreeNode tree)// C++
bool isBalanced(TreeNode *tree)
The function isBalanced returns true because the tree is balanced.