Expression Tree | Practice | GeeksforGeeks

Given a full binary expression tree consisting of basic binary operators (+ , – ,*, /) and some integers, Your task is to evaluate the expression tree.
Example 1:
Input: 
              +
           /     \
          *       -
        /  \    /   \
       5    4  100  20 

Output: 100

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

Example 2:
Input:
            -
          /   \
         4     7

Output: -3

Explanation:
4 - 7 = -3

Your Task:  
You dont need to read input or print anything. Complete the function evalTree() which takes root node as input parameter and returns an integer denoting the result obtained by simplifying the expression tree.

Expected Time Complexity: O(N)
Expected Auxiliary Space: O(height of tree)

Constraints:
1 ≤ N ≤ 500
 
0
bansallakshay0813 days ago
    /*You are required to complete below method */   int evalTree(node* root) {       // Your code here             if(root->left==NULL&&root->right==NULL) return stoi(root->data);              if (root->data=="+")return evalTree(root->left)+evalTree(root->right);       if (root->data=="-")return evalTree(root->left)-evalTree(root->right);       if (root->data=="*")return evalTree(root->left)*evalTree(root->right);       if (root->data=="/")return evalTree(root->left)/evalTree(root->right);   }
0
suyashsingh33 weeks ago
Java Solution

 if(root==null) return 0;
       
       if(root.left==null && root.right==null) return Integer.parseInt(root.data);
       
       
       switch(root.data){
           case "+" : return evalTree(root.left)+evalTree(root.right);
           case "-": return evalTree(root.left)-evalTree(root.right);
           case "*": return evalTree(root.left)*evalTree(root.right);
           case "/": return evalTree(root.left)/evalTree(root.right);
       }
       
       return 0;
0
detroix072 months ago
int evalTree(node* root) {
     // base case      if(root==NULL) return 0;     // postorder traversal      int left = evalTree(root->left);     int right = evalTree(root->right);          if(root->data== "+") return left+right;     if(root->data== "-") return left-right;     if(root->data== "*") return left*right;     if(root->data== "/") return left/right;     return stoi(root->data);   }
+2
tanujrajput00003 months ago
int evalTree(node* root) {
        if(root==NULL)
            return 0;
        int l = evalTree(root->left);
        int r = evalTree(root->right);
        
        if(root->data=="*")return l*r;
        else if(root->data=="+")return l+r;
        else if(root->data=="/")return l/r;
        else if(root->data=="-")return l-r;
        else return stoi(root->data);
        
        

    }
+1
ashwinkumar99313 months ago
int evalTree(node* root) {       if(root==NULL){           return 0;       }              if(root->left==NULL&&root->right==NULL){           int x = 0;           stringstream geek(root->data);           geek >> x;           return x;       }              //evaluaveare left and right              int left = evalTree(root->left);       int right = evalTree(root->right);              if(root->data == "+"){           return left+right;       }       if(root->data == "*"){           return left*right;       }       if(root->data == "-"){           return left-right;       }       if(root->data == "/"){           return left/right;       }   }
+1
aaishuagarwal3 months ago
    int evalTree(node* root) {       // Your code here       if(root->left==NULL&&root->right==NULL)       return stoi(root->data);       int l,r;       if(root->right)       r=evalTree(root->right);       if(root->left)       l=evalTree(root->left);              if(root->data=="+")       {         int ans=l+r;         return ans;       }       else if(root->data=="-")       return l-r;       else if(root->data=="*")       return l*r;       else if(root->data=="/")       return l/r;       else if(root->data=="%")       return l%r;   }
0
tarun2002ts0213 months ago
int evalTree(node* root) {       // Your code here       if(root->left==NULL && root->right==NULL)       return stoi(root->data);       int ans=evalTree(root->left);       int ans1=evalTree(root->right);            if(root->data=="+")      return ans+ans1;      else if(root->data=="-")      return ans-ans1;       else if(root->data=="*")      return ans*ans1;       else      return ans/ans1;   }
0
hamidnourashraf3 months ago
def evalTree(self, root):
       if root.left is None and root.right is None:
           return int(root.data)
       left_val = self.evalTree(root.left)
       right_val = self.evalTree(root.right)
       if root.data == '+':
           return left_val + right_val
       if root.data == '-':
           return left_val - right_val
       if root.data == '*':
           return left_val * right_val
       if root.data == '/':
           return int(left_val / right_val)
+2
shubh003 months ago
c++ solution...
Taken time - 0.0/1.2 sec
 
class Solution{ public:   /*You are required to complete below method */   int evalTree(node* root) {       // Your code here       string val = root -> data;       if(root == NULL){         return 0;       }       else if(val == "+"){         int res = evalTree(root -> left) + evalTree(root -> right);         return res;       }       else if(val == "-"){         int res = evalTree(root -> left) - evalTree(root -> right);         return res;       }       else if(val == "*"){         int res = evalTree(root -> left) * evalTree(root -> right);         return res;       }       else if(val == "/"){         int res = evalTree(root -> left) / evalTree(root -> right);         return res;       }       else{         return stoi(root -> data);       }   }};
0
badgujarsachin834 months ago
int evalTree(node* root) {
        // Your code here
        if(root->left==NULL && root->right==NULL){
            return stoi(root->data);
        }
        if(root->data=="+"){
            return evalTree(root->left)+evalTree(root->right);
        }
        if(root->data=="-"){
            return evalTree(root->left)-evalTree(root->right);
        }
        if(root->data=="*"){
            return evalTree(root->left)*evalTree(root->right);
        }
        if(root->data=="/"){
            return evalTree(root->left)/evalTree(root->right);
        }
    }
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.