HackerRank Huffman Decoding(Huffman解碼)

題目鏈接

/* 
The structure of the node is

typedef struct node
{
    int freq;
    char data;
    node * left;
    node * right;
    
}node;

*/


void decode_huff(node * root,string s)
{
    char str[50];
    int i,j,len;
    node *p;
    len=s.length();
    for(i=0,j=0;i<len;i++){
        p=root;
        while(p){
            if(s[i]=='0'){
                p=p->left;
            }
            else{
                p=p->right;
            }
            if(!p->left&&!p->right){
                str[j++]=p->data;
                break;
            }
            else{
                i++;
            }
        }
    }
    str[j]='\0';
    printf("%s\n",str);
}


發佈了41 篇原創文章 · 獲贊 78 · 訪問量 10萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章