Evaluate expression tree.
Given a simple expression tree, which is also a full binary tree consisting of basic binary operators i.e., + , – ,* and / and some integers.
Algorithm:
- Traverse Given Binary Tree in PostOrder
- If
node is null
thenreturn 0
- If
node is leaf node
thenreturn node.data
- leftValue = recursively call left substree with node.left
- rightValue = recursively call right substree with node.right
value = perform binary arithmetic operation on leftValue and rightValue with using
- return value
- If
Latest Source Code:
Github: EvaluateExpressionTree.java
Output:
+ / \ / \ / \ / \ + 10 / \ / \ 30 * / \ 10 20 Value is : 240