Dsa
Binary Search Tree
Medium
Subtree Imbalance Factor
The 'Imbalance Factor' of a node in a BST is the absolute difference between the total number of nodes in its left subtree and the total number of nodes in its right subtree. Given a BST and a target node value X, calculate the Imbalance Factor of the node containing X. If X is not in the tree, return -1.
Input: First line contains BST nodes. Second line contains X.
Output: The absolute difference in counts or -1.
Example:
Input:
10 5 15 2 7 12 20
10
Output: 0
Explanation: Node 10 has 3 nodes on the left (5, 2, 7) and 3 on the right (15, 12, 20). |3-3| = 0.
Key concepts
binary_search_treerecursiontree_node_count
Practise this out loud — free
Start a mock interview on THIS exact question — a voice AI interviewer opens with it, pushes back like a real onsite, then hands you an instant scorecard.
🎙 Practise this question now