Tree: Huffman Decoding

  • + 0 comments

    Python solution:

    def decodeHuff(root: Node, s: str) -> None:
        result = []
        curr = root    # start pointer at root node
        for digit in s:
            curr = curr.left if digit == "0" else curr.right
            # if a leaf node is reached, append its character to decoded  
            # string result, and reset pointer back to root of tree
            if not all([curr.left, curr.right]):    # equivalent to `is not None`
                result.append(curr.data)
                curr = root     
        print("".join(result))
    

    Instructions should have been clearer on the "output" of the function either returning or printing the string.