检查一颗树是否为二叉查找树

// 检查一颗树是否为二叉查找树
#include <iostream>
#include <stack>
using namespace std;
#define MIN_NUM -0x80000000
struct Node {
  int val;
  Node* left;
  Node* right;
};

bool CheckBinarySearchTree(Node* root) {
  if (!root) return false;
  stack<Node*> s;
  int pre = MIN_NUM;
  Node* cur = root;
  while (cur || !s.empty()) {
    while (cur) {
      s.push(cur);
      cur = cur->left;
    }

    if (!s.empty()) {
      Node* top = s.top();
      if (top->val < pre) {
        return false;
      }
      pre = top->val;
      s.pop();
      cur = top->right;
    }
  }
  return true;
}

 

posted @ 2013-09-13 01:40  dmthinker  阅读(112)  评论(0)    收藏  举报