A binary expression tree is a kind of binary tree used to represent arithmetic expressions. Each node of a binary expression tree has either zero or two children. Leaf nodes (nodes with 0 children) correspond to operands (numbers), and internal nodes (nodes with 2 children) correspond to the operators '+' (addition), '-' (subtraction), '*' (multiplication), and '/' (division).
For each internal node with operator o, the infix expression it represents is (A o B), where A is the expression the left subtree represents and B is the expression the right subtree represents.
You are given a string s, an infix expression containing operands, the operators described above, and parentheses '(' and ')'.
Return _any validbinary expression tree , whose in-order traversal reproduces _safter omitting the parenthesis from it.
Please note that order of operations applies ins. That is, expressions in parentheses are evaluated first, and multiplication and
division happen before addition and subtraction.
Operands must also appear in the same order in both s and the in-order traversal of the tree.
Input: s ="3*4-2*5"Output: [-,*,*,3,4,2,5]Explanation: The tree above is the only valid tree whose inorder traversal produces s.
Example 2:
1
2
3
4
5
6
7
Input: s ="2-3/(5*2)+1"Output: [+,-,1,2,/,null,null,null,null,3,*,null,null,5,2]Explanation: The inorder traversal of the tree above is2-3/5*2+1 which is the same as s without the parenthesis. The tree also produces the correct result and its operands are in the same order as they appear in s.The tree below is also a valid binary expression tree with the same inorder traversal as s, but it not a valid answer because it does not evaluate to the same value.
The third tree below is also not valid. Although it produces the same result and is equivalent to the above trees, its inorder traversal does not produce s and its operands are not in the same order as s.
Example 3:
1
2
3
Input: s ="1+2+3+4+5"Output: [+,+,5,+,4,null,null,+,3,null,null,1,2]Explanation: The tree [+,+,5,+,+,null,null,1,2,3,4]is also one of many other valid trees.
Constraints:
1 <= s.length <= 100
s consists of digits and the characters '(', ')', '+', '-', '*', and '/'.
We use the shunting yard algorithm to convert the infix expression to a binary expression tree. We use two stacks: one for operators and one for tree nodes. When we see an operand, we create a node. When we see an operator, we pop nodes and build subtrees according to precedence and associativity.
classNode:
def__init__(self, val, left=None, right=None):
self.val = val
self.left = left
self.right = right
classSolution:
defbuildTree(self, s: str) ->'Node':
defprecedence(op):
if op in ('+', '-'): return1if op in ('*', '/'): return2return0 ops, nodes = [], []
i =0while i < len(s):
if s[i].isdigit():
nodes.append(Node(s[i]))
i +=1elif s[i] =='(':
ops.append('(')
i +=1elif s[i] ==')':
while ops and ops[-1] !='(': build()
ops.pop()
i +=1else:
while ops and ops[-1] !='('and precedence(ops[-1]) >= precedence(s[i]):
build()
ops.append(s[i])
i +=1while ops: build()
return nodes[-1]
defbuild():
op = ops.pop()
r = nodes.pop()
l = nodes.pop()
nodes.append(Node(op, l, r))