Convert Ternary Expression to Binary Tree
Submissions: 1849   Accuracy:

59.19%

  Difficulty: Easy   Marks: 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

** For More Input/Output Examples Use 'Expected Output' option **

Contributor: Saksham Raj Seth
Author: saksham seth


Need help with your code? Please use ide.geeksforgeeks.org, generate link and share the link here.

to report an issue on this page.