In this problem, you will determine the imbalance value of a node in a tree. If a node is null or if a node's left and right subtrees have the same height, its imbalance value is 0. If a node's left subtree's height is greater than its right subtree's height, it has a negative imbalance value equal to the number of levels higher the left subtree is than the right subtree. If a node's right subtree's height is greater than its left subtree's height, it has a positive imbalance value equal to the number of levels higher the right subtree is than the left subtree.
For example, if a node's right subtree has a height of 8 and its left subtree has a height of 3, its imbalance value is 5. If a node's left subtree has a height of 4 and its right subtree has a height of 3, its imbalance value is -1.
The following image shows two trees, where each node is labeled with its imbalance value:
(0)
(0)
(0)
Complete the balance method shown in the code below:
Purpose: Get the imbalance value of the given node
Parameters: TreeNode n - the node
Returns: int - the imbalance value
public int imbalance(TreeNode n) {
// TODO: implement this
Similar to TreeNodes we have worked with throughout the term, you can access each TreeNode's left and right child. You will likely need to make a helper method to implement the imbalance method correctly.