# 2021-11-08每日刷题打卡

## 力扣——二叉搜索树

#### 98. 验证二叉搜索树和面试题 04.05. 合法二叉搜索树

``````/**
* Definition for a binary tree node.
* struct TreeNode {
*     int val;
*     TreeNode *left;
*     TreeNode *right;
*     TreeNode() : val(0), left(nullptr), right(nullptr) {}
*     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
*     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
bool flag=true;
bool isValidBST(TreeNode* root) {
if(!root->left&&!root->right)return flag;
dfs(root,NULL,NULL);
return flag;
}
void dfs(TreeNode* root,TreeNode* max,TreeNode* min)
{
if(!flag||!root)return;

if(max&&root->val >= max->val)flag=false;
if(min&&root->val <= min->val)flag=false;

if(root->left)dfs(root->left,root,min);
if(root->right)dfs(root->right,max,root);
}
};
``````

``````/**
* Definition for a binary tree node.
* struct TreeNode {
*     int val;
*     TreeNode *left;
*     TreeNode *right;
*     TreeNode() : val(0), left(nullptr), right(nullptr) {}
*     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
*     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
vector<int>v;
bool isValidBST(TreeNode* root) {
if(!root->left&&!root->right)return true;
dfs(root);
int n=v.size();
for(int i=0;i<n-1;i++)
{
if(v[i]>=v[i+1])
return false;
}
return true;
}
void dfs(TreeNode* root)
{
if(!root)return;
dfs(root->left);
v.push_back(root->val);
dfs(root->right);
}
};
``````

#### 面试题 04.02. 最小高度树和108. 将有序数组转换为二叉搜索树

``````      0
/
-3   9
/   /
-10  5
``````

``````/**
* Definition for a binary tree node.
* struct TreeNode {
*     int val;
*     TreeNode *left;
*     TreeNode *right;
*     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
TreeNode* sortedArrayToBST(vector<int>& nums) {
TreeNode*root;
dfs(root,nums);
return root;
}
void dfs(TreeNode*&root,vector<int>nums)
{
int n=nums.size();
if(!n)return;
int len=n/2;
vector<int>v1;
vector<int>v2;
root=new TreeNode(nums[len]);
for(int i=0;i<len;i++)
v1.push_back(nums[i]);
for(int i=len+1;i<n;i++)
v2.push_back(nums[i]);

dfs(root->left,v1);
dfs(root->right,v2);
}
};
``````

#### 109. 有序链表转换二叉搜索树

``````  0
/
``````

-3 9
/ /
-10 5

（当然，也可以一遍一遍二叉树一遍遍历链表，只要用快慢指针就可以求得链表的中间值了）

``````/**
* struct ListNode {
*     int val;
*     ListNode *next;
*     ListNode() : val(0), next(nullptr) {}
*     ListNode(int x) : val(x), next(nullptr) {}
*     ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
/**
* Definition for a binary tree node.
* struct TreeNode {
*     int val;
*     TreeNode *left;
*     TreeNode *right;
*     TreeNode() : val(0), left(nullptr), right(nullptr) {}
*     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
*     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
TreeNode* root;
vector<int>v;
{
}
dfs(root,v);
return root;
}
void dfs(TreeNode*&root,vector<int>nums)
{
int n=nums.size();
if(!n)return;
int len=n/2;
root=new TreeNode(nums[len]);
vector<int>v1;
vector<int>v2;
for(int i=0;i<len;i++)
v1.push_back(nums[i]);
for(int i=len+1;i<n;i++)
v2.push_back(nums[i]);
dfs(root->left,v1);
dfs(root->right,v2);
}
};
``````

#### 99. 恢复二叉搜索树

``````/**
* Definition for a binary tree node.
* struct TreeNode {
*     int val;
*     TreeNode *left;
*     TreeNode *right;
*     TreeNode() : val(0), left(nullptr), right(nullptr) {}
*     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
*     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
vector<int>v;
void recoverTree(TreeNode* root) {
dfs(root);
int i=0;
sort(v.begin(),v.end());
dfs1(root,i);
}
void dfs(TreeNode*&root)
{
if(!root)return;
dfs(root->left);
v.push_back(root->val);
dfs(root->right);
}
void dfs1(TreeNode*&root,int &i)
{
if(!root)return;
dfs1(root->left,i);
root->val=v[i++];
dfs1(root->right,i);
}
};
``````

#### 1008. 前序遍历构造二叉搜索树

(回想一下，二叉搜索树是二叉树的一种，其每个节点都满足以下规则，对于 node.left 的任何后代，值总 < node.val，而 node.right 的任何后代，值总 > node.val。此外，前序遍历首先显示节点 node 的值，然后遍历 node.left，接着遍历 node.right。）

``````/**
* Definition for a binary tree node.
* struct TreeNode {
*     int val;
*     TreeNode *left;
*     TreeNode *right;
*     TreeNode() : val(0), left(nullptr), right(nullptr) {}
*     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
*     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
TreeNode* bstFromPreorder(vector<int>& preorder) {
TreeNode* root;
dfs(root,preorder);
return root;
}
void dfs(TreeNode*&root,vector<int>preorder)
{
if(!preorder.size())return;
vector<int>v1;
vector<int>v2;
int num=preorder[0],n=preorder.size();
for(int i=1;i<n;i++)
if(preorder[i]<num)
v1.push_back(preorder[i]);
else
v2.push_back(preorder[i]);
root=new TreeNode(num);
dfs(root->left,v1);
dfs(root->right,v2);
}
};
``````

THE END