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
belongs to collection: Interview C++ coding problems/challenges | String
All Answers
total answers (1)

C++ programming
Algorithm:
//recursive function to build the tree FUNCTION convertExpression(string str, int& i) 1. Create root referring to the current character(str[i]); root =newNode(str,i); //create a node with element str[i] 2. Increment i (index that point to current character); //if i<string length and current token is '?' increment i 3. IF(i<str.length() && str[i]=='?'){ //need to build left child recursively increment i root->left=convertExpression(str,i); //need to build right child recursively root->right=convertExpression(str,i); 4. return root;Algorithm with example:
C++ implementation
Output
need an explanation for this answer? contact us directly to get an explanation for this answer