Amazon Interview Question Software Engineer / Developers
0of 0 votesexpression tree design.
This code has bug
ab+cd* gets converted to two subtrees a + b and c * d but these two subtrees are not connected and program would return only the subtree at the top.

I think what the question means if - if you are given an infix/postfix expression, make an expression tree out of it.
Eg. Convert a postfix expression into an expression tree
Algorithm:
1. Intialize a stack to store binary tree nodes.
2. Read the array from left to right.
3. If you come across an operand, make a node out of it and push into the stack.
If its an operator, pop two nodes from the stack and first popped node would be the operator node's right child and the second popped node would be the operator node's left child. Push the operator node into the stack
4.The last popped node would be the head of the tree.
- Dragon on March 04, 2012 Edit | Flag Replystruct node *createTree(int a[]) { Stack S; struct node *new = NULL; for(int i = 0; a[i]! = ‘\0’; i++) { if (arr[i] == ‘+’ || arr[i] == ‘-’ || arr[i] == ‘/’ || arr[i] == ‘*’) { new = newNode(a[i]); new -> right = s1.pop(); new->left = s1.pop(); s1.push(new); } else s1.push(newNode(a[i]); } } return s1.pop(); }