Evaluation of Expression Tree

Last Updated : 19 Jun, 2026

Given a full binary expression tree consisting of basic binary operators (+, - , *, /) and some integers. Evaluate the value of expression tree and return.

Examples:

Input: root = [+, *, -, 5, 4, 100, 20]

2056958259

Output: 100
Explanation: ((5 * 4) + (100 - 20)) = 100

Input: root = [-, 4, 7]

2056958258

Output: -3
Explanation: 4 - 7 = -3

Using Inorder Traversal : O(n) Time and O(h) Space Complexity

As all the operators in the tree are binary, hence each node will have either 0 or 2 children. As it can be inferred from the examples above, all the integer values would appear at the leaf nodes, while the interior nodes represent the operators. Therefore we do inorder traversal of the binary tree and evaluate the expression as we move upward in the tree.

Algorithm: Evaluate Expression Tree

  • If the tree is empty (root == NULL), return 0.
  • If the current node is a leaf node (operand), convert its value to an integer and return it.
  • Recursively evaluate the left and right subtrees and store the result in l_val and r_val respectively.
  • Check the operator stored at the current node (+, -, *, /).
  • Apply the operator on l_val and r_val, and return the computed result.
C++
#include <iostream>
#include <string>
using namespace std;

/* Definition for Node */
class Node
{
  public:
    string data;
    Node *left;
    Node *right;

    Node(string val)
    {
        data = val;
        left = right = nullptr;
    }
};

int evalTree(Node *root)
{
    // Empty tree
    if (!root)
        return 0;

    // Leaf node (operand)
    if (!root->left && !root->right)
        return stoi(root->data);

    // Evaluate left and right subtrees
    int l_val = evalTree(root->left);
    int r_val = evalTree(root->right);

    // Apply operator
    if (root->data == "+")
        return l_val + r_val;

    if (root->data == "-")
        return l_val - r_val;

    if (root->data == "*")
        return l_val * r_val;

    return l_val / r_val;
}

int main()
{
    /*
            +
          /   \
         *     -
        / \   / \
       5   4 100 20

       Expression: (5 * 4) + (100 - 20)
       Result = 100
    */

    Node *root = new Node("+");

    root->left = new Node("*");
    root->right = new Node("-");

    root->left->left = new Node("5");
    root->left->right = new Node("4");

    root->right->left = new Node("100");
    root->right->right = new Node("20");

    cout << evalTree(root) << endl;

    return 0;
}
Java
import java.util.*;

/* Definition for Node */
class Node {
    String data;
    Node left;
    Node right;

    Node(String val)
    {
        data = val;
        left = right = null;
    }
}

public class GFG {

    static int evalTree(Node root)
    {
        // Empty tree
        if (root == null)
            return 0;

        // Leaf node (operand)
        if (root.left == null && root.right == null)
            return Integer.parseInt(root.data);

        // Evaluate left and right subtrees
        int l_val = evalTree(root.left);
        int r_val = evalTree(root.right);

        // Apply operator
        if (root.data.equals("+"))
            return l_val + r_val;

        if (root.data.equals("-"))
            return l_val - r_val;

        if (root.data.equals("*"))
            return l_val * r_val;

        return l_val / r_val;
    }

    public static void main(String[] args)
    {

        /*
                   +
                 /   \
                *     -
               / \   / \
              5   4 100 20

           Expression: (5 * 4) + (100 - 20)
           Result = 100
        */

        Node root = new Node("+");

        root.left = new Node("*");
        root.right = new Node("-");

        root.left.left = new Node("5");
        root.left.right = new Node("4");

        root.right.left = new Node("100");
        root.right.right = new Node("20");

        System.out.println(evalTree(root));
    }
}
Python
# Definition for Node
class Node:
    def __init__(self, val):
        self.data = val
        self.left = None
        self.right = None


def evalTree(root):
    # Empty tree
    if not root:
        return 0

    # Leaf node (operand)
    if not root.left and not root.right:
        return int(root.data)

    # Evaluate left and right subtrees
    l_val = evalTree(root.left)
    r_val = evalTree(root.right)

    # Apply operator
    if root.data == "+":
        return l_val + r_val

    if root.data == "-":
        return l_val - r_val

    if root.data == "*":
        return l_val * r_val

    return int(l_val / r_val)


# Driver Code
if __name__ == "__main__":
    """
               +
             /   \
            *     -
           / \   / \
          5   4 100 20

       Expression: (5 * 4) + (100 - 20)
       Result = 100
    """

    root = Node("+")

    root.left = Node("*")
    root.right = Node("-")

    root.left.left = Node("5")
    root.left.right = Node("4")

    root.right.left = Node("100")
    root.right.right = Node("20")

    print(evalTree(root))
C#
using System;

/* Definition for Node */
class Node {
    public string data;
    public Node left;
    public Node right;

    public Node(string val)
    {
        data = val;
        left = right = null;
    }
}

class GFG {
    static int evalTree(Node root) {
        // Empty tree
        if (root == null)
            return 0;

        // Leaf node (operand)
        if (root.left == null && root.right == null)
            return int.Parse(root.data);

        // Evaluate left and right subtrees
        int l_val = evalTree(root.left);
        int r_val = evalTree(root.right);

        // Apply operator
        if (root.data == "+")
            return l_val + r_val;

        if (root.data == "-")
            return l_val - r_val;

        if (root.data == "*")
            return l_val * r_val;

        return l_val / r_val;
    }

    static void Main()
    {
        /*
                   +
                 /   \
                *     -
               / \   / \
              5   4 100 20

           Expression: (5 * 4) + (100 - 20)
           Result = 100
        */

        Node root = new Node("+");

        root.left = new Node("*");
        root.right = new Node("-");

        root.left.left = new Node("5");
        root.left.right = new Node("4");

        root.right.left = new Node("100");
        root.right.right = new Node("20");

        Console.WriteLine(evalTree(root));
    }
}
JavaScript
/* Definition for Node */
class Node {
    constructor(val)
    {
        this.data = val;
        this.left = null;
        this.right = null;
    }
}

function evalTree(root)
{
    // Empty tree
    if (root === null) {
        return 0;
    }

    // Leaf node (operand)
    if (root.left === null && root.right === null) {
        return Number(root.data);
    }

    // Evaluate left and right subtrees
    let lVal = evalTree(root.left);
    let rVal = evalTree(root.right);

    let res;

    // Apply operator
    if (root.data === "+") {
        res = lVal + rVal;
    }
    else if (root.data === "-") {
        res = lVal - rVal;
    }
    else if (root.data === "*") {
        res = lVal * rVal;
    }
    else {

        // C++ integer division truncates towards zero.
        // Math.trunc() provides the same behavior in
        // JavaScript.
        res = Math.trunc(lVal / rVal);
    }

    // JavaScript may produce -0 (e.g. Math.trunc(-1 / 2)).
    // Convert it to 0 so that the output matches C++.
    return Object.is(res, -0) ? 0 : res;
}

// Driver Code

/*
           +
         /   \
        *     -
       / \   / \
      5   4 100 20

   Expression: (5 * 4) + (100 - 20)
   Result = 100
*/

let root = new Node("+");

root.left = new Node("*");
root.right = new Node("-");

root.left.left = new Node("5");
root.left.right = new Node("4");

root.right.left = new Node("100");
root.right.right = new Node("20");

console.log(evalTree(root));

Output
100
Comment