Tree: Huffman Decoding

  • + 0 comments
    def decodeHuff(root, s):
    	#Enter Your Code Here
        s = list(s)
        current = root
        results = []
        
        def traverse(current, s):
            if len(s) > 0:
                if s[0] == "0" and current.left is not None:
                    s.pop(0)
                    traverse(current.left, s)
                elif s[0] == "1" and current.right is not None:
                    s.pop(0)
                    traverse(current.right, s)
            
            if current.left is None and current.right is None:
                results.append(current.data)
    
        while len(s) > 0:
            traverse(current, s)
        
        print("".join(results))