Question bank › Binary Search Tree
Dsa Binary Search Tree Medium

Maximum Shadow Weight

The 'Shadow Weight' of a node in a BST is defined as the absolute difference between the total number of nodes in its left subtree and the total number of nodes in its right subtree. Build a BST from a sequence of unique integers. Find the maximum Shadow Weight among all nodes in the tree and print the value of the node that carries this weight. If multiple nodes have the same maximum weight, print the one with the smallest numerical value. Input Format: - A single line of space-separated integers. Output Format: - The integer value of the node with the highest Shadow Weight. Example: Input: 10 5 15 2 Output: 10 Explanation: Node 10 has Left Subtree Size 2 (5, 2) and Right Subtree Size 1 (15). Weight = |2-1| = 1. Node 5 has Left Size 1, Right Size 0. Weight = |1-0| = 1. Node 10 and 5 both have weight 1. 5 is smaller, but wait, the root 10 has weight 1 as well. Let's re-calc: 10 has (5,2) on left, (15) on right. Diff is 1. 5 has (2) on left, () on right. Diff is 1. Max weight is 1, smallest node is 2 (weight 0), 5 (weight 1), 10 (weight 1), 15 (weight 0). Answer: 5.

Key concepts

binary_search_treerecursionsubtree_analysis

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
Part of InterviewLab's verified interview question bank. We show the prompt and concepts to practise with — never a copy-paste solution.