列表

详情


1628. 设计带解析函数的表达式树

给定一个算术表达式的后缀表示法的标记(token) postfix ,构造并返回该表达式对应的二叉表达式树。

后缀表示法是一种将操作数写在运算符之前的表示法。例如,表达式 4*(5-(2+7)) 的后缀表示法表示为数组 postfix = ["4","5","7","2","+","-","*"] 。

抽象类 Node 需要用于实现二叉表达式树。我们将通过 evaluate 函数来测试返回的树是否能够解析树中的值。你不可以移除 Node 类,但你可以按需修改此类,也可以定义其他类来实现它。

二叉表达式树是一种表达算术表达式的二叉树。二叉表达式树中的每一个节点都有零个或两个子节点。 叶节点(有 0 个子节点的节点)表示操作数,非叶节点(有 2 个子节点的节点)表示运算符: '+' (加)、 '-' (减)、 '*' (乘)和 '/' (除)。

我们保证任何子树对应值的绝对值不超过 109 ,且所有操作都是有效的(即没有除以零的操作)

进阶: 你可以将表达式树设计得更模块化吗?例如,你的设计能够不修改现有的 evaluate 的实现就能支持更多的操作符吗?

 

示例 1:

输入: s = ["3","4","+","2","*","7","/"]
输出: 2
解释: 此表达式可解析为上述二叉树,其对应表达式为 ((3+4)*2)/7) = 14/7 = 2.

示例 2:

输入: s = ["4","5","7","2","+","-","*"]
输出: -16
解释: 此表达式可解析为上述二叉树,其对应表达式为 4*(5-(2+7)) = 4*(-4) = -16.

 

提示:

原站题解

去查看

上次编辑到这里,代码来自缓存 点击恢复默认模板
/** * This is the interface for the expression tree Node. * You should not remove it, and you can define some classes to implement it. */ class Node { public: virtual ~Node () {}; virtual int evaluate() const = 0; protected: // define your fields here }; /** * This is the TreeBuilder class. * You can treat it as the driver code that takes the postinfix input * and returns the expression tree represnting it as a Node. */ class TreeBuilder { public: Node* buildTree(vector<string>& postfix) { } }; /** * Your TreeBuilder object will be instantiated and called as such: * TreeBuilder* obj = new TreeBuilder(); * Node* expTree = obj->buildTree(postfix); * int ans = expTree->evaluate(); */

javascript 解法, 执行用时: 76 ms, 内存消耗: 41.6 MB, 提交时间: 2023-10-16 21:40:12

/**
 * This is the interface for the expression tree Node.
 * You should not remove it, and you can define some classes to implement it.
 */

var Node = function () {
  if (this.constructor === Node) {
    throw new Error('Cannot instanciate abstract class')
  }
}

Node.prototype.evaluate = function () {
  throw new Error('Cannot call abstract method')
}

class TreeNode extends Node {
  constructor(val, left, right) {
    super()
    this.val = val === undefined ? ' ' : val
    this.left = left === undefined ? null : left
    this.right = right === undefined ? null : right
  }

  evaluate() {
    return postorder(this)

    function postorder(root) {
      if (root === null) return 0
      const l = postorder(root.left)
      const r = postorder(root.right)
      if (/\d/.test(root.val)) return root.val
      switch (root.val) {
        case '*':
          return l * r
        case '/':
          return l / r
        case '+':
          return l + r
        case '-':
          return l - r
      }
    }
  }
}

/**
 * This is the TreeBuilder class.
 * You can treat it as the driver code that takes the postinfix input
 * and returns the expression tree represnting it as a Node.
 */

class TreeBuilder {
  /**
   * @param {string[]} s
   * @return {Node}
   */
  buildTree(postfix) {
    const stack = []
    const reg = /\d/
    for (const s of postfix) {
      if (reg.test(s)) {
        stack.push(new TreeNode(+s))
      } else {
        const a = stack.pop()
        const b = stack.pop()
        stack.push(new TreeNode(s, b, a))
      }
    }
    return stack[0]
  }
}

/**
 * Your TreeBuilder object will be instantiated and called as such:
 * var obj = new TreeBuilder();
 * var expTree = obj.buildTree(postfix);
 * var ans = expTree.evaluate();
 */

python3 解法, 执行用时: 52 ms, 内存消耗: 16.2 MB, 提交时间: 2023-10-16 21:39:35

import abc 
from abc import ABC, abstractmethod 
"""
This is the interface for the expression tree Node.
You should not remove it, and you can define some classes to implement it.
"""

class Node():
    def __init__(self, val):
        self.val = val
        self.left = None
        self.right = None
    
    def evaluate(self) -> int:
        if self.val.isdigit():
            return int(self.val)
        if self.val == '+':
            return self.left.evaluate() + self.right.evaluate()
        if self.val == '-':
            return self.left.evaluate() - self.right.evaluate()
        if self.val == '*':
            return self.left.evaluate() * self.right.evaluate()
        if self.val == '/':
            return self.left.evaluate() // self.right.evaluate()

"""    
This is the TreeBuilder class.
You can treat it as the driver code that takes the postinfix input
and returns the expression tree represnting it as a Node.
"""

class TreeBuilder(object):
    def buildTree(self, postfix: List[str]) -> 'Node':
        if not postfix: return None
        node = Node(postfix.pop())
        if node.val.isdigit():
            return node
        node.right = self.buildTree(postfix)
        node.left = self.buildTree(postfix)
        return node
		
"""
Your TreeBuilder object will be instantiated and called as such:
obj = TreeBuilder();
expTree = obj.buildTree(postfix);
ans = expTree.evaluate();
"""

cpp 解法, 执行用时: 8 ms, 内存消耗: 8.5 MB, 提交时间: 2023-10-16 21:38:36

/**
 * This is the interface for the expression tree Node.
 * You should not remove it, and you can define some classes to implement it.
 */

class Node {
public:
    Node(const string& val) : val(val), left(nullptr), right(nullptr) {}
    virtual ~Node () {};
    virtual int evaluate() const
    {
        if (val == "+")
        {
            return left->evaluate() + right->evaluate();
        }
        else if (val == "-")
        {
            return left->evaluate() - right->evaluate();
        }
        else if (val == "*")
        {
            return left->evaluate() * right->evaluate();
        }
        else if (val == "/")
        {
            return left->evaluate() / right->evaluate();
        }
        else
        {
            return stoi(val);
        }
    }

protected:
    // define your fields here
    string val;
    Node* left;
    Node* right;

    friend class TreeBuilder;
};


/**
 * This is the TreeBuilder class.
 * You can treat it as the driver code that takes the postinfix input 
 * and returns the expression tree represnting it as a Node.
 */

class TreeBuilder {
private:
    stack<Node*> s;
public:
    Node* buildTree(vector<string>& postfix) {
        for (const string& str : postfix)
        {
            Node* n = new Node(str);
            if (str == "+" || str == "-" || str == "*" || str == "/")
            {
                n->right = s.top();
                s.pop();
                n->left = s.top();
                s.pop();
            }
            s.push(n);
        }
        return s.top();
    }
};


/**
 * Your TreeBuilder object will be instantiated and called as such:
 * TreeBuilder* obj = new TreeBuilder();
 * Node* expTree = obj->buildTree(postfix);
 * int ans = expTree->evaluate();
 */

java 解法, 执行用时: 1 ms, 内存消耗: 39.1 MB, 提交时间: 2023-10-16 21:38:14

/**
 * This is the interface for the expression tree Node.
 * You should not remove it, and you can define some classes to implement it.
 */

abstract class Node {
    public abstract int evaluate();
    // define your fields here
};


/**
 * This is the TreeBuilder class.
 * You can treat it as the driver code that takes the postinfix input 
 * and returns the expression tree represnting it as a Node.
 */

class TreeBuilder {
    Node buildTree(String[] postfix) {
        Stack<BiOpNode> stack = new Stack<>();
        for (String s : postfix) {
            BiOpNode node = new BiOpNode(s);
            if ("+".equals(s) || "-".equals(s) || "*".equals(s) || "/".equals(s)) {
                node.right = stack.pop();
                node.left = stack.pop();
            }
            stack.push(node);
        }
        return stack.pop();
    }

    class BiOpNode extends Node {
        String val;
        BiOpNode left;
        BiOpNode right;

        BiOpNode(String val) {
            this.val = val;
        }

        @Override
        public int evaluate() {
            switch (val) {
                case "+":
                    return left.evaluate() + right.evaluate();
                case "-":
                    return left.evaluate() - right.evaluate();
                case"*":
                    return left.evaluate() * right.evaluate();
                case"/":
                    return left.evaluate() / right.evaluate();
                default:
                    return Integer.valueOf(val);
            }
        }
    }
};


/**
 * Your TreeBuilder object will be instantiated and called as such:
 * TreeBuilder obj = new TreeBuilder();
 * Node expTree = obj.buildTree(postfix);
 * int ans = expTree.evaluate();
 */

上一题