forked from terrytong0876/LintCode-1
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConvert Expression to Polish Notation.java
More file actions
executable file
·124 lines (106 loc) · 3.4 KB
/
Copy pathConvert Expression to Polish Notation.java
File metadata and controls
executable file
·124 lines (106 loc) · 3.4 KB
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
H
还是Expression Tree (Min-Tree).
根据题意,Tree出来以后,来个Pre-order-traversal.
Note: label需要是String.虽然 Operator是长度为1的char, 但是数字可为多位。
```
/*
Given an expression string array, return the Polish notation of this expression. (remove the parentheses)
Have you met this question in a real interview? Yes
Example
For the expression [(5 − 6) * 7] (which represented by ["(", "5", "−", "6", ")", "*", "7"]), the corresponding polish notation is [* - 5 6 7] (which the return value should be ["*", "−", "5", "6", "7"]).
Clarification
Definition of Polish Notation:
http://en.wikipedia.org/wiki/Polish_notation
http://baike.baidu.com/view/7857952.htm
Tags Expand
LintCode Copyright Stack
*/
/*
Thoughts:
Build the expression tree, and do a pre-order-traversal, and record all nodes in the array list.
Let's practice expression tree build again.
*/
public class Solution {
class TreeNode {
String s;
int val;
TreeNode left;
TreeNode right;
public TreeNode(int val, String s) {
this.val = val;
this.s = s;
this.left = null;
this.right = null;
}
}
public TreeNode build(String[] expression) {
if (expression == null || expression.length == 0) {
return null;
}
Stack<TreeNode> stack = new Stack<TreeNode>();
int base = 0;
int val = 0;
for (int i = 0; i < expression.length; i++) {
if (expression[i].equals("(")) {
base += 10;
continue;
}
if (expression[i].equals(")")) {
base -= 10;
continue;
}
val = getWeight(base, expression[i]);
TreeNode node = new TreeNode(val, expression[i]);
while (!stack.isEmpty() && node.val <= stack.peek().val) {
node.left = stack.pop();
}
if (!stack.isEmpty()) {
stack.peek().right = node;
}
stack.push(node);
}
if (stack.isEmpty()) {
return null;
}
TreeNode rst = stack.pop();
while (!stack.isEmpty()) {
rst = stack.pop();
}
return rst;
}
public int getWeight(int base, String s) {
if (s.equals("+") || s.equals("-")) {
return base + 1;
}
if (s.equals("*") || s.equals("/")) {
return base + 2;
}
return Integer.MAX_VALUE;
}
/**
* @param expression: A string array
* @return: The Polish notation of this expression
*/
public ArrayList<String> convertToPN(String[] expression) {
ArrayList<String> rst = new ArrayList<String>();
if (expression == null || expression.length == 0) {
return rst;
}
TreeNode root = build(expression);
preTraversal(rst, root);
return rst;
}
public void preTraversal(ArrayList<String> rst, TreeNode node){
if (node == null) {
return;
}
if (node.left == null && node.right == null) {
rst.add(node.s);
return;
}
rst.add(node.s);
preTraversal(rst, node.left);
preTraversal(rst, node.right);
}
}
```