仔细检查(函数名是否相同,变量名是否相同,if语句是否有问题,返回值类型)
1、标点符号(分号)
2、判断语句(==)
3、函数输入形参是否相互对应(一定要仔细检查!!)
4、循环有没有越界
5、循环中不变量有没有改变(例如链表题中size有没有变化)
**
617题:合并二叉树
**
使用递归解题,前序遍历,注意写清楚函数!!!
**
700:二叉搜索树
**
使用递归方法,分别查找
98:验证二叉搜索树
中序遍历输出数组,判断数组是否有序即可!!!
仔细检查(函数名是否相同,变量名是否相同,if语句是否有问题,返回值类型)
530:二叉搜索树的最小绝对差
将二叉搜索树转化为数组,从而通过数组求最小绝对差
/**
* 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:
void travel(TreeNode* node,vector<int>& re){
if(node==NULL) return;
travel(node->left,re);
re.push_back(node->val);
travel(node->right,re);
}
int getMinimumDifference(TreeNode* root) {
vector<int> re;
travel(root,re);
int x=INT_MAX;
if(re.size()==0||re.size()==1) return 0;
for(int i=0;i<re.size()-1;i++){
x=min(x,abs(re[i]-re[i+1]));
}
return x;
}
};
501题:二叉搜索树的众数
使用遍历求解
/**
* 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:
int maxCount;
int count;
TreeNode* pre;
vector<int> result;
void searchBST(TreeNode* cur) {
if (cur == NULL) return ;
searchBST(cur->left); // 左
if (pre == NULL) { // 第一个节点
count = 1;
} else if (pre->val == cur->val) { // 与前一个节点数值相同
count++;
} else { // 与前一个节点数值不同
count = 1;
}
pre = cur; // 更新上一个节点
if (count == maxCount) { // 如果和最大值相同,放进result中
result.push_back(cur->val);
}
if (count > maxCount) { // 如果计数大于最大值频率
maxCount = count; // 更新最大频率
result.clear(); // 很关键的一步,不要忘记清空result,之前result里的元素都失效了
result.push_back(cur->val);
}
searchBST(cur->right); // 右
return ;
}
vector<int> findMode(TreeNode* root) {
count = 0;
maxCount = 0;
TreeNode* pre = NULL; // 记录前一个节点
result.clear();
searchBST(root);
return result;
}
};
236:二叉树的公共祖先
使用回溯
/**
* 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* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
if(root==p||root==q||root==NULL) return root;
TreeNode* left=lowestCommonAncestor(root->left,p,q);
TreeNode* right=lowestCommonAncestor(root->right,p,q);
if(left!=NULL&&right!=NULL) return root;
else if(left==NULL&&right!=NULL) return right;
else if(right==NULL&&left!=NULL) return left;
else return NULL;
}
};
235:二叉搜索树的公共祖先问题
二叉搜索树,利用大小关系性质
/**
* 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* lo(TreeNode* node,TreeNode* p,TreeNode* q){
if(node==NULL) return node;
if(node==p||node==q) return node;
if(node->val>p->val&&node->val<q->val) return node;
if(node->val<p->val&&node->val<q->val) return lo(node->right,p,q);
else if(node->val>q->val&&node->val>p->val) return lo(node->left,p,q);
return NULL;
}
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
if(p->val>q->val) return lo(root,q,p);
else return lo(root,p,q);
}
};
701:二叉搜索树的插入操作
使用递归
/**
* 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* travel(TreeNode* node,int val){
if(node==NULL) {
TreeNode* node=new TreeNode(val);
return node;
}
if(node->val>val) node->left=travel(node->left,val);
if(node->val<val) node->right=travel(node->right,val);
return node;
}
TreeNode* insertIntoBST(TreeNode* root, int val) {
return travel(root,val);
}
};
450:删除二叉搜索树节点
需要调整树的结构,分情况讨论
/**
* 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* deleteNode(TreeNode* root, int key) {
if(root==NULL) return root;
if(root->val==key) {
if(root->left==NULL) return root->right;
else if(root->right==NULL) return root->left;
else{
TreeNode* cur=root->right;
while(cur->left!=NULL) cur=cur->left;
cur->left=root->left;
TreeNode* tmp=root;
root=root->right;
delete tmp;
return root;
}
}
if(root->val>key) root->left=deleteNode(root->left,key);
if(root->val<key) root->right=deleteNode(root->right,key);
return root;
}
};
669:修建二叉搜索树
本题使用递归,但是一定要判断清楚情况,不能遗漏!第一次写的时候没有考虑到边界等于的情况。
/**
* 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* trimBST(TreeNode* root, int low, int high) {
if(root==NULL) return NULL;
if(root->val>high) return trimBST(root->left,low,high);
if(root->val<low) return trimBST(root->right,low,high);
if(root->val>=low&&root->val<=high){
root->left=trimBST(root->left,low,high);
root->right=trimBST(root->right,low,high);;
}
return root;
}
};
108:将有序数组转化为二叉搜索树(构造二叉搜索树)
使用递归,同样要注意边界条件和特殊值,第一次没有考虑nums.size()为1的情况!!考虑全面
/**
* 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* sortedArrayToBST(vector<int>& nums) {
if(nums.size()==0) return NULL;
TreeNode* node=new TreeNode(0);
int index=nums.size()/2;
node->val=nums[index];
if(nums.size()==1) return node;
vector<int> numsl(nums.begin(),nums.begin()+index);
vector<int> numsr(nums.begin()+index+1,nums.end());
node->left=sortedArrayToBST(numsl);
node->right=sortedArrayToBST(numsr);
return node;
}
};
同样需要每次定义vector,后续优化空间使用率,直接使用索引!
538:把二叉搜索树转化为累加树
初步想的是遍历两次,然后取数组和map进行存储,但是时间和空间消耗都比较大
/**
* 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:
void travel(TreeNode* node,vector<int>& re){
if(node==NULL) return;
travel(node->left,re);
re.push_back(node->val);
travel(node->right,re);
}
void travelt(TreeNode* node,map<int,int>& map){
if(node==NULL) return;
travelt(node->left,map);
int x=node->val;
node->val=map[x];
travelt(node->right,map);
}
TreeNode* convertBST(TreeNode* root) {
vector<int> re;
travel(root,re);
map<int,int> map;
int ins=0;
for(int i=re.size()-1;i>=0;i--){
ins+=re[i];
map[re[i]]=ins;
}
travelt(root,map);
return 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:
int ans;
void travel(TreeNode* node){
if(node==NULL) return;
travel(node->right);
ans+=node->val;
node->val=ans;
travel(node->left);
}
TreeNode* convertBST(TreeNode* root) {
if(root==NULL) return NULL;
ans=0;
travel(root);
return root;
}
};