EVALUATE EXPRESSION TREE

Problem - All leaf node in the tree are operands, always positive number. All the other nodes represents operator.

-1 represents addition

-2  : subtract

-3 : division

-4 : product


APPROACH : think about base case. If you are on leaf node then return.

    i.e. if tree.value > 0 : return the value .

    next simply get the left tree value and right tree value and apply the operand respectively using recursion.

    

solution evaluation expression tree 



Comments

Popular posts from this blog

MINIMUM HEIGHT BST

CHECK IF GIVEN BINARY TREE IS A BST/ VALIDATE BST

Kth LARGEST ELEMENT IN BST