diff options
author | Haidong Ji | 2019-03-23 21:12:13 -0500 |
---|---|---|
committer | Haidong Ji | 2019-03-23 21:12:13 -0500 |
commit | bf7d8fa962098eeafc4eed84e4ae0450806a43c3 (patch) | |
tree | f1f0a1f9cee1fcc9c275a967ae3889c05d19df30 /src/main | |
parent | acff8424e09a3531cae111a93fb0f6fce1d4404e (diff) |
check in post-order test cases
Diffstat (limited to 'src/main')
-rw-r--r-- | src/main/TreeTraversal.java | 18 |
1 files changed, 12 insertions, 6 deletions
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(); } } } |