forked from rajatgoyal715/Hackerrank
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTree:HuffmanDecoding.java
More file actions
31 lines (29 loc) · 824 Bytes
/
Tree:HuffmanDecoding.java
File metadata and controls
31 lines (29 loc) · 824 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
/*
class Node
public int frequency; // the frequency of this tree
public char data;
public Node left, right;
*/
void decode(String S ,Node root)
{
StringBuilder ans = new StringBuilder();
int i=0;
while(i<S.length()){
Node temp = root;
while(i<S.length() && temp!=null){
char c = S.charAt(i);
if(c == '1'){
temp = temp.right;
} else if(c == '0') {
temp = temp.left;
}
if(temp.left == null && temp.right == null){
ans.append(temp.data);
break;
}
i++;
}
i++;
}
System.out.println(ans.toString());
}