From bf7d8fa962098eeafc4eed84e4ae0450806a43c3 Mon Sep 17 00:00:00 2001 From: Haidong Ji Date: Sat, 23 Mar 2019 21:12:13 -0500 Subject: check in post-order test cases --- src/main/TreeTraversal.java | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) (limited to 'src/main') diff --git a/src/main/TreeTraversal.java b/src/main/TreeTraversal.java index fee494c..d69164e 100644 --- a/src/main/TreeTraversal.java +++ b/src/main/TreeTraversal.java @@ -125,8 +125,15 @@ public class TreeTraversal { currentIndex = left[currentIndex]; keyIndexStack.push(currentIndex); } else { - result.add(key[keyIndexStack.pop()]); - walkLeft = false; + if (right[currentIndex] == -1) { + result.add(key[currentIndex]); + walkLeft = false; + keyIndexStack.pop(); + currentIndex = keyIndexStack.peek(); + } else { + currentIndex = right[currentIndex]; + keyIndexStack.push(currentIndex); + } } } else { if (right[currentIndex] != -1) { @@ -134,10 +141,9 @@ public class TreeTraversal { keyIndexStack.push(currentIndex); walkLeft = true; } else { - if (!keyIndexStack.empty()) { - currentIndex = keyIndexStack.pop(); - result.add(key[currentIndex]); - } + result.add(key[currentIndex]); + keyIndexStack.pop(); + currentIndex = keyIndexStack.peek(); } } } -- cgit v1.2.3