检查一颗树是否为二叉查找树
// 检查一颗树是否为二叉查找树 #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; }
Passion, patience, perseverance, keep it and move on.

浙公网安备 33010602011771号