Huffman coding encodes characters based on their frequency. Each letter is assigned a variable-length binary string, where shorter lengths correspond to more common letters. A binary tree is built such that the path from root to any leaf maps to a character: descending left = 0, descending right = 1. Given character frequencies, build a Huffman tree and determine the encoding mapping for each character. Output each character and its binary encoding, sorted alphabetically by character.
Example 1:
Example 2:
Constraints:
1 <= number of distinct characters <= 256 1 <= frequency <= 10^6
Tags:
