二叉查找树

来源:互联网 发布:python sleep(.1) 编辑:程序博客网 时间:2024/05/21 00:15
class Solution {public:    /**     * @param root: The root of binary tree.     * @return: True if the binary tree is BST, or false     */    bool isValidBST(TreeNode *root) {        // write your code here        if (root == nullptr) {            return true;        }        vector<int> result;        in_trva(root, result);        for(int i = 0; i != result.size() - 1; ++i) {            if (result[i] >= result[i+1]){                return false;            }        }        return true;    }   void in_trva(TreeNode *x, vector<int> &ret) {       if (x == nullptr){           return;       }       in_trva(x -> left, ret);       ret.push_back(x -> val);       in_trva(x -> right, ret);   }};

0 0
原创粉丝点击