We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
voiddecode(Strings,Noderoot){StringBuildersb=newStringBuilder();NodetempNode=root;// Loop over all chars (1's and 0's)for(charc:s.toCharArray()){// Check if the current Node is the end of a branchif(tempNode.left==null&&tempNode.right==null){// Append data to sbsb.append(tempNode.data);// reset positiontempNode=root;}// travel down a branch using the current char (1 or 0)if(Character.getNumericValue(c)==0){tempNode=tempNode.left;}else{tempNode=tempNode.right;}}// Append remaining data if we finish at the end of a branchif(tempNode.left==null&&tempNode.right==null){sb.append(tempNode.data);}// output our build stringSystem.out.println(sb.toString());}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Tree: Huffman Decoding
You are viewing a single comment's thread. Return to all comments →
Java Solution: