分两次dfs
判断一棵二叉树是否为搜索二叉树和完全二叉树
http://www.nowcoder.com/questionTerminal/f31fc6d3caf24e7f8b4deb5cd9b5fa97
/** * struct TreeNode { * int val; * struct TreeNode *left; * struct TreeNode *right; * }; */ class Solution { public: /** * * @param root TreeNode类 the root * @return bool布尔型vector */ vector<bool> judgeIt(TreeNode* root) { // write code here return {isSearchTree(root,INT_MIN,INT_MAX),isCompletedTree(root)}; } bool isCompletedTree(TreeNode* root) { if(root == nullptr) return true; if(root->left && root->right == nullptr) return isCompletedTree(root->left); if(root->left == nullptr && root->right) return false; return isCompletedTree(root->left) && isCompletedTree(root->right); } bool isSearchTree(TreeNode *root, int left, int right) { if(root == nullptr) return true; if(root->val < left || root->val > right) return false; return isSearchTree(root->left,left,root->val) && isSearchTree(root->right, root->val,right); } };