Convert Ternary Expression to Binary Tree
Easy Accuracy: 58.46% Submissions: 8945 Points: 2

Given a string that contains ternary expressions. The expressions may be nested. You need to convert the given ternary expression to a binary Tree and return the root.

Input Format:
First line of input contains of test case T. The only line of test case consists of String s.

Outpu Format:
Preorder traversal of Tree formed

Your Task:
This is a function problem, you don't need to read input/output. Just complete the function convertExpression that take string and index(initialized from 0) as parameters.

Constraints:
1 <= T <= 100
1 <= |String| <= 100

Example:
Input:

2
a?b:c
a?b?c:d:e
Output:
a b c
a b c d e

Explanation:
Testcase1:

Input :  string expression =   a?b:c
Output : a
              /  \
             b    c
Testcase2:

Input : expression =  a?b?c:d:e
Output :   a
                /  \
              b    e
             /  \
            c    d

to report an issue on this page.

Editorial

We strongly recommend solving this problem on your own before viewing its editorial. Do you still want to view the editorial?

Yes

All Submissions

My Submissions:

Login to access your submissions.