Amazon | Phone screen | Arithmetic Expression Tree

Question Arithmetic Expression Tree (AET) is a specific kind of a binary tree used to represent expressions.
Each node of a binary expression tree has zero or two children.

         (/)
         / \
        /   \
      (*)   (4)
      / \
     /   \
   (2)   (+)
         / \
        /   \
      (3)   (/)
            / \
           /   \
         (6)   (2)
         
        (/)
         / \
        /   \
      (8)   (4)
  1. Design and code a data structure to represent arbitrarily large and complex arithmetic expressions.
    Consider maintainability and extensibility of your structure.
  2. Implement evaluate.
My answer
class Node {
     int key;
     Node left, right;
     public Node (int key) {
         this.key = key;
     }
     
     public abstract float evaluate();
 }
 
 class DivideNode extends Node {
     public float evaluate() {
         return this.left / this.right; 
         
     }
     
 }
 
 class AddNode extends Node {
     public float evaluate() {
         return this.left + this.right; 
     }
 }
 
 class ScalarNode extends Node {
     public float evaluate() {
         return key; 
     }
 }
Comments (22)