https://github.com/TouwaErioH/subjects/tree/master/C%2B%2B/PA2
BST
假设已经给定树节点的结构不可修改。
然后输入是按照层次顺序
怎样创建BST?
1. 当input number较小时,先把输入的数存到数组里,然后从第一个数开始递归创建即可,(左孩子2K,右孩子2K+1)
或者用指针数组,存当前层的节点,然后插入其孩子,得到下一层的所有节点,然后再插入下下层。
2.当number很大 2^20-1时,就不太合适了。思路为用 int path[21]数组存储当前点路径 。假设根节点编号为1,当前要插入第13个数,就先求得13,6,3,1放到path数组里,到1然后1,3,6,13找到位置插入这个点。
创建并判断BST(number较小的情况)
#include <iostream> #include <math.h> #include <algorithm> using namespace std; // Definition for a binary tree node. struct TreeNode { int val; int max; // maximum value of the subtree from this node int min; // minimum value of the subtree from this node int num; // number of the node TreeNode* left; TreeNode* right; TreeNode(int x) : val(x), max(NULL), min(NULL), left(NULL), right(NULL), num(NULL) {} TreeNode() : val(NULL), max(NULL), min(NULL), left(NULL), right(NULL), num(NULL){} }; bool is_BST(TreeNode* root){ if (root->left == NULL&&root->right == NULL) { return true; } if (is_BST(root->left) && is_BST(root->right) ) { if(root->left->max==0) { if(root->right->min==0) return true; else if(root->right->min> root->val) return true; else return false; } else{ if(root->left->max < root->val) { if(root->right->min==0) return true; else if(root->right->min> root->val) return true; else return false; } else return false; } } else { return false; } } void buildnode(TreeNode* root, int A[], int n) { if (2 * root->num <= n) { root->left = new TreeNode; root->left->val = A[2 * root->num - 1]; root->left->num = 2 * root->num; root->right = new TreeNode; root->right->val = A[2 * root->num]; root->right->num = 2 * root->num + 1; } else { return; } buildnode(root->left, A, n); buildnode(root->right, A, n); } int fillmax(TreeNode* root) { if (root->right == NULL) { root->max = root->val; return root->max; } else { root->max = max(max(fillmax(root->right), root->val), fillmax(root->left)); return root->max; } } int fillmin(TreeNode* root) { if (root->left == NULL) { root->min = root->val; return root->min; } else { if (root->left->val == 0 && root->right->val != 0) { root->min = min(root->val, fillmin(root->right)); } if (root->left->val != 0 && root->right -> val == 0) { root->min = min(root->val, fillmin(root->left)); } if (root->left->val == 0 && root->right -> val == 0) { root->min = root->val; }
if (root->left->val != 0 && root->right -> val != 0) {
root->min = min(min(root->val, fillmin(root->left)), fillmin(root->right));
}
return root->min; } } void printtree(TreeNode* root) { if (root == NULL) { return; } cout << root->min<<endl; printtree(root->left); printtree(root->right); if (root->left == NULL || root->right == NULL) { return; } } void SolveD() { TreeNode* root = new TreeNode; int n; cin >> n; int* valarray = new int[n]; int value; for (int i = 0; i <= (n - 1); i++) { cin >> value; valarray[i] = value; } root->val = valarray[0]; root->num = 1; buildnode(root, valarray, n); fillmin(root); fillmax(root); //printtree(root); if (is_BST(root)) { cout << "true"; return; } else { cout << "false"; return; } return; } int main() { SolveD(); system("pause"); return 0; }
这是复杂但是符合题目要求的方法。
还可以直接递归
/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ class Solution { public: bool isValidBST(TreeNode *root) { return isValidBST(root, INT_MIN, INT_MAX); } bool isValidBST(TreeNode *root, int low, int high){ if (root == NULL ) return true; if (low < root->val && root->val < high) return (isValidBST(root->left, low, root->val) && isValidBST(root->right, root->val, high)); else return false; } };
或者先序遍历
/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ class Solution { public: bool isValidBST(TreeNode *root) { vector<int> res; isValidBST(root, res); int len = res.size(); bool flag = true; for (int i=0; i<len-1; i++){ if (res[i] >= res[i+1]){ flag = false; break; } } return flag; } void isValidBST(TreeNode *root, vector<int> &res){ if (root == NULL) return; isValidBST(root->left, res); res.push_back(root->val); isValidBST(root->right, res); } };