@@ -238,7 +238,7 @@ class Solution {
238
238
while (! st. empty()) {
239
239
TreeNode node = st. peek();
240
240
if (node != null ) {
241
- st. pop(); // 将该节点弹出,避免重复操作,下面再将右中左节点添加到栈中
241
+ st. pop(); // 将该节点弹出,避免重复操作,下面再将右左中节点添加到栈中(前序遍历-中左右,入栈顺序右左中)
242
242
if (node. right!= null ) st. push(node. right); // 添加右节点(空节点不入栈)
243
243
if (node. left!= null ) st. push(node. left); // 添加左节点(空节点不入栈)
244
244
st. push(node); // 添加中节点
@@ -266,11 +266,10 @@ public List<Integer> inorderTraversal(TreeNode root) {
266
266
while (! st. empty()) {
267
267
TreeNode node = st. peek();
268
268
if (node != null ) {
269
- st. pop(); // 将该节点弹出,避免重复操作,下面再将右中左节点添加到栈中
269
+ st. pop(); // 将该节点弹出,避免重复操作,下面再将右中左节点添加到栈中(中序遍历-左中右,入栈顺序右中左)
270
270
if (node. right!= null ) st. push(node. right); // 添加右节点(空节点不入栈)
271
271
st. push(node); // 添加中节点
272
272
st. push(null ); // 中节点访问过,但是还没有处理,加入空节点做为标记。
273
-
274
273
if (node. left!= null ) st. push(node. left); // 添加左节点(空节点不入栈)
275
274
} else { // 只有遇到空节点的时候,才将下一个节点放进结果集
276
275
st. pop(); // 将空节点弹出
@@ -294,7 +293,7 @@ class Solution {
294
293
while (! st. empty()) {
295
294
TreeNode node = st. peek();
296
295
if (node != null ) {
297
- st. pop(); // 将该节点弹出,避免重复操作,下面再将右中左节点添加到栈中
296
+ st. pop(); // 将该节点弹出,避免重复操作,下面再将中右左节点添加到栈中(后序遍历-左右中,入栈顺序中右左)
298
297
st. push(node); // 添加中节点
299
298
st. push(null ); // 中节点访问过,但是还没有处理,加入空节点做为标记。
300
299
if (node. right!= null ) st. push(node. right); // 添加右节点(空节点不入栈)
@@ -975,3 +974,4 @@ public IList<int> PostorderTraversal(TreeNode root)
975
974
<a href =" https://programmercarl.com/other/kstar.html " target =" _blank " >
976
975
<img src =" ../pics/网站星球宣传海报.jpg " width =" 1000 " />
977
976
</a >
977
+
0 commit comments