Huffman Decoding – Hackerrank

Problem Statement

Refer: https://www.hackerrank.com/challenges/tree-huffman-decoding/problem

Solution

void decode(String s, Node root) {

        StringBuilder sb = new StringBuilder();

         Node c = root;
        for(int i=0;i<s.length();i++)
        {
           c = s.charAt(i) == '1' ? c.right : c.left;

           if(c.left == null && c.right == null)
           {
               sb.append(c.data);
               c = root;
           }
        }
        System.out.println(sb);

    }

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s