回答編集履歴

1

追記

2021/12/21 03:10

投稿

kazuma-s
kazuma-s

スコア8224

test CHANGED
@@ -59,3 +59,65 @@
59
59
  a b * c +
60
60
 
61
61
  ```
62
+
63
+
64
+
65
+ **追記**
66
+
67
+ StringBuilder に追加していきたいのなら、
68
+
69
+ ```Java
70
+
71
+ class Node {
72
+
73
+ public String expression; // 演算子または項
74
+
75
+ public Node left = null; // 左の子ノード
76
+
77
+ public Node right = null; // 右の子ノード
78
+
79
+
80
+
81
+ Node(String e) { expression = e; }
82
+
83
+ Node(String e, Node l, Node r) { expression = e; left = l; right = r; }
84
+
85
+
86
+
87
+ public void traversePostorder(StringBuilder rt) {
88
+
89
+ if (left != null) left.traversePostorder(rt);
90
+
91
+ if (right != null) right.traversePostorder(rt);
92
+
93
+ rt.append(expression + " ");
94
+
95
+ }
96
+
97
+ }
98
+
99
+
100
+
101
+ class Main {
102
+
103
+ public static void main(String args[]) {
104
+
105
+ Node root = new Node("+",
106
+
107
+ new Node("*", new Node("a"), new Node("b")),
108
+
109
+ new Node("c")
110
+
111
+ );
112
+
113
+ StringBuilder sb = new StringBuilder();
114
+
115
+ root.traversePostorder(sb);
116
+
117
+ System.out.println(sb);
118
+
119
+ }
120
+
121
+ }
122
+
123
+ ```