[LeetCode] Build Binary Expression Tree From Infix Expression

1597. Build Binary Expression Tree From Infix Expression

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 that 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 valid binary expression tree, which its in-order traversal reproduces s after omitting the parenthesis from it (see examples below).

Please note that order of operations applies in s. 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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/**
* Definition for a binary tree node.
* struct Node {
* char val;
* Node *left;
* Node *right;
* Node() : val(' '), left(nullptr), right(nullptr) {}
* Node(char x) : val(x), left(nullptr), right(nullptr) {}
* Node(char x, Node *left, Node *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
Node* build(string& s, int l, int r) {
Node* op = new Node(s[l + (r-l)/2]);
op->left = new Node(s[l]);
op->right = new Node(s[r]);
return op;
}
int find(string& s, int l, int r, char op1, char op2) {
int st = 0;
for(int i = r; i >= l; i--) {
if(s[i] == '(') st++;
else if(s[i] == ')') st--;
else if(st == 0 and (s[i] == op1 or s[i] == op2)) return i;
}
return -1;
}
int findFirstOutBound(string& s, int l, int r) {
int st = 1;
for(int i = l + 1; i <= r; i++) {
if(s[i] == '(') st++;
else if(s[i] == ')') st--;
if(st == 0) return i;
}
return r;
}
Node* helper(string& s, int l, int r) {
if(r - l + 1 == 3) return build(s,l,r);
if(l == r) return new Node(s[l]);
int p = find(s, l, r,'+','-');
if(p != -1) {
Node* op = new Node(s[p]);
op->left = helper(s,l,p-1);
op->right = helper(s,p+1,r);
return op;
} else {
if(s[l] == '(') {
int boundp = findFirstOutBound(s,l,r);
if(boundp == r)
return helper(s,l+1,r-1);
Node* op = new Node(s[boundp + 1]);
op->left = helper(s,l+1,boundp-1);
op->right = helper(s,boundp+2,r);
return op;
} else {
int opp = find(s,l,r,'*','/');
Node* op = new Node(s[opp]);
op->left = helper(s,l,opp-1);
op->right = helper(s,opp+1,r);
return op;
}
}

}
public:
Node* expTree(string s) {
return helper(s,0,s.length() - 1);
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2022/04/07/PS/LeetCode/build-binary-expression-tree-from-infix-expression/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.