From 56c652e96dcedf3acbe6f37d9540044b2681b2da Mon Sep 17 00:00:00 2001 From: Haidong Ji Date: Wed, 12 Jun 2019 20:22:10 -0500 Subject: check if binary search tree harder version done! Comparing this against my Java and Python code, one more change was needed: key value type changed from int to long. In reality, I'm not sure if int would have worked, but I decided to be better safe than sorry.--- Sources/DataStructure.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Sources/DataStructure.cpp b/Sources/DataStructure.cpp index ebb4c2e..e9332c4 100644 --- a/Sources/DataStructure.cpp +++ b/Sources/DataStructure.cpp @@ -13,7 +13,7 @@ using std::endl; class TreeOrders { public: int n; - vector key; + vector key; vector left; vector right; @@ -30,7 +30,7 @@ public: bool is_Bst() { if (key.size()==0) return true; std::stack keyIndexStack; - vector result; + vector result; bool walkLeft = true; int currentIndex = 0; keyIndexStack.push(currentIndex); @@ -44,14 +44,14 @@ public: int i = keyIndexStack.top(); keyIndexStack.pop(); if (result.size() > 0) { - if (key[i] <= result[(result.size()-1)]) return false; + if (key[i] < result[(result.size()-1)]) return false; } result.push_back(key[i]); walkLeft = false; } } else { if (right[currentIndex] != -1) { - if (key[right[currentIndex]] <= key[currentIndex]) return false; + if (key[right[currentIndex]] < key[currentIndex]) return false; currentIndex = right[currentIndex]; keyIndexStack.push(currentIndex); walkLeft = true; -- cgit v1.2.3