My solution.

English description:

I start with a stack data structure with an empty node in it and iterated through the expression once. For every character, I follow this rule.

1. if '?', peek at the top of stack and initialize left node. Push left node to stack

2. if ':', pop the top node in the stack, then peek at the top. Initialize right node and push right node to stack

3. if character, peek at top node and set value to that node

Node ternaryToTree(String exp) {
Stack stack = new Stack();
Node node = new Node();
stack.push(node);
for(int i = 0; i < exp.length(); i++) {
char c = exp.charAt(i);
if(c == '?') {
stack.peek().left = new Node();
stack.push(stack.peek().left);
} else if(c == ':') {
stack.pop();
stack.peek().right = new Node();
stack.push(stack.peek().right);
} else {
stack.peek().val = c;
}
}
return node;
}