|
64 | 64 | <li>保证表达式不包含除以零的操作。</li>
|
65 | 65 | </ul>
|
66 | 66 |
|
67 |
| - |
68 | 67 | ## 解法
|
69 | 68 |
|
70 | 69 | <!-- 这里可写通用的实现逻辑 -->
|
|
76 | 75 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
77 | 76 |
|
78 | 77 | ```python
|
79 |
| - |
| 78 | +import abc |
| 79 | +from abc import ABC, abstractmethod |
| 80 | +""" |
| 81 | +This is the interface for the expression tree Node. |
| 82 | +You should not remove it, and you can define some classes to implement it. |
| 83 | +""" |
| 84 | + |
| 85 | +class Node(ABC): |
| 86 | + @abstractmethod |
| 87 | + # define your fields here |
| 88 | + def evaluate(self) -> int: |
| 89 | + pass |
| 90 | + |
| 91 | +class MyNode(Node): |
| 92 | + |
| 93 | + def __init__(self, val): |
| 94 | + self.val = val |
| 95 | + self.left = None |
| 96 | + self.right = None |
| 97 | + |
| 98 | + def evaluate(self) -> int: |
| 99 | + x = self.val |
| 100 | + if x.isdigit(): |
| 101 | + return int(x) |
| 102 | + |
| 103 | + left, right = self.left.evaluate(), self.right.evaluate() |
| 104 | + if x == '+': |
| 105 | + return left + right |
| 106 | + if x == '-': |
| 107 | + return left - right |
| 108 | + if x == '*': |
| 109 | + return left * right |
| 110 | + if x == '/': |
| 111 | + return left // right |
| 112 | + |
| 113 | + |
| 114 | +""" |
| 115 | +This is the TreeBuilder class. |
| 116 | +You can treat it as the driver code that takes the postinfix input |
| 117 | +and returns the expression tree represnting it as a Node. |
| 118 | +""" |
| 119 | + |
| 120 | +class TreeBuilder(object): |
| 121 | + def buildTree(self, postfix: List[str]) -> 'Node': |
| 122 | + stk = [] |
| 123 | + for s in postfix: |
| 124 | + node = MyNode(s) |
| 125 | + if not s.isdigit(): |
| 126 | + node.right = stk.pop() |
| 127 | + node.left = stk.pop() |
| 128 | + stk.append(node) |
| 129 | + return stk[-1] |
| 130 | + |
| 131 | +""" |
| 132 | +Your TreeBuilder object will be instantiated and called as such: |
| 133 | +obj = TreeBuilder(); |
| 134 | +expTree = obj.buildTree(postfix); |
| 135 | +ans = expTree.evaluate(); |
| 136 | +""" |
80 | 137 | ```
|
81 | 138 |
|
82 | 139 | ### **Java**
|
83 | 140 |
|
84 | 141 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
85 | 142 |
|
86 | 143 | ```java
|
| 144 | +/** |
| 145 | + * This is the interface for the expression tree Node. |
| 146 | + * You should not remove it, and you can define some classes to implement it. |
| 147 | + */ |
| 148 | + |
| 149 | +abstract class Node { |
| 150 | + public abstract int evaluate(); |
| 151 | + // define your fields here |
| 152 | + protected String val; |
| 153 | + protected Node left; |
| 154 | + protected Node right; |
| 155 | +}; |
| 156 | + |
| 157 | +class MyNode extends Node { |
| 158 | + public MyNode(String val) { |
| 159 | + this.val = val; |
| 160 | + } |
| 161 | + |
| 162 | + public int evaluate() { |
| 163 | + if (isNumeric()) { |
| 164 | + return Integer.parseInt(val); |
| 165 | + } |
| 166 | + int leftVal = left.evaluate(); |
| 167 | + int rightVal = right.evaluate(); |
| 168 | + if (Objects.equals(val, "+")) { |
| 169 | + return leftVal + rightVal; |
| 170 | + } |
| 171 | + if (Objects.equals(val, "-")) { |
| 172 | + return leftVal - rightVal; |
| 173 | + } |
| 174 | + if (Objects.equals(val, "*")) { |
| 175 | + return leftVal * rightVal; |
| 176 | + } |
| 177 | + if (Objects.equals(val, "/")) { |
| 178 | + return leftVal / rightVal; |
| 179 | + } |
| 180 | + return 0; |
| 181 | + } |
| 182 | + |
| 183 | + public boolean isNumeric() { |
| 184 | + for (char c : val.toCharArray()) { |
| 185 | + if (!Character.isDigit(c)) { |
| 186 | + return false; |
| 187 | + } |
| 188 | + } |
| 189 | + return true; |
| 190 | + } |
| 191 | +} |
| 192 | + |
| 193 | + |
| 194 | +/** |
| 195 | + * This is the TreeBuilder class. |
| 196 | + * You can treat it as the driver code that takes the postinfix input |
| 197 | + * and returns the expression tree represnting it as a Node. |
| 198 | + */ |
| 199 | + |
| 200 | +class TreeBuilder { |
| 201 | + Node buildTree(String[] postfix) { |
| 202 | + Deque<MyNode> stk = new ArrayDeque<>(); |
| 203 | + for (String s : postfix) { |
| 204 | + MyNode node = new MyNode(s); |
| 205 | + if (!node.isNumeric()) { |
| 206 | + node.right = stk.pop(); |
| 207 | + node.left = stk.pop(); |
| 208 | + } |
| 209 | + stk.push(node); |
| 210 | + } |
| 211 | + return stk.peek(); |
| 212 | + } |
| 213 | +}; |
| 214 | + |
| 215 | + |
| 216 | +/** |
| 217 | + * Your TreeBuilder object will be instantiated and called as such: |
| 218 | + * TreeBuilder obj = new TreeBuilder(); |
| 219 | + * Node expTree = obj.buildTree(postfix); |
| 220 | + * int ans = expTree.evaluate(); |
| 221 | + */ |
| 222 | +``` |
87 | 223 |
|
| 224 | +### **C++** |
| 225 | + |
| 226 | +```cpp |
| 227 | +/** |
| 228 | + * This is the interface for the expression tree Node. |
| 229 | + * You should not remove it, and you can define some classes to implement it. |
| 230 | + */ |
| 231 | + |
| 232 | +class Node { |
| 233 | +public: |
| 234 | + virtual ~Node () {}; |
| 235 | + virtual int evaluate() const = 0; |
| 236 | +protected: |
| 237 | + // define your fields here |
| 238 | + string val; |
| 239 | + Node* left; |
| 240 | + Node* right; |
| 241 | +}; |
| 242 | + |
| 243 | +class MyNode : public Node { |
| 244 | +public: |
| 245 | + MyNode(string val) { |
| 246 | + this->val = val; |
| 247 | + } |
| 248 | + |
| 249 | + MyNode(string val, Node* left, Node* right) { |
| 250 | + this->val = val; |
| 251 | + this->left = left; |
| 252 | + this->right = right; |
| 253 | + } |
| 254 | + |
| 255 | + int evaluate() const { |
| 256 | + if (!(val == "+" || val == "-" || val == "*" || val == "/")) return stoi(val); |
| 257 | + auto leftVal = left->evaluate(), rightVal = right->evaluate(); |
| 258 | + if (val == "+") return leftVal + rightVal; |
| 259 | + if (val == "-") return leftVal - rightVal; |
| 260 | + if (val == "*") return leftVal * rightVal; |
| 261 | + if (val == "/") return leftVal / rightVal; |
| 262 | + return 0; |
| 263 | + } |
| 264 | +}; |
| 265 | + |
| 266 | + |
| 267 | +/** |
| 268 | + * This is the TreeBuilder class. |
| 269 | + * You can treat it as the driver code that takes the postinfix input |
| 270 | + * and returns the expression tree represnting it as a Node. |
| 271 | + */ |
| 272 | + |
| 273 | +class TreeBuilder { |
| 274 | +public: |
| 275 | + Node* buildTree(vector<string>& postfix) { |
| 276 | + stack<MyNode*> stk; |
| 277 | + for (auto s : postfix) |
| 278 | + { |
| 279 | + MyNode* node; |
| 280 | + if (s == "+" || s == "-" || s == "*" || s == "/") |
| 281 | + { |
| 282 | + auto right = stk.top(); |
| 283 | + stk.pop(); |
| 284 | + auto left = stk.top(); |
| 285 | + stk.pop(); |
| 286 | + node = new MyNode(s, left, right); |
| 287 | + } |
| 288 | + else |
| 289 | + { |
| 290 | + node = new MyNode(s); |
| 291 | + } |
| 292 | + stk.push(node); |
| 293 | + } |
| 294 | + return stk.top(); |
| 295 | + } |
| 296 | +}; |
| 297 | + |
| 298 | + |
| 299 | +/** |
| 300 | + * Your TreeBuilder object will be instantiated and called as such: |
| 301 | + * TreeBuilder* obj = new TreeBuilder(); |
| 302 | + * Node* expTree = obj->buildTree(postfix); |
| 303 | + * int ans = expTree->evaluate(); |
| 304 | + */ |
88 | 305 | ```
|
89 | 306 |
|
90 | 307 | ### **...**
|
|
0 commit comments