此篇博客按照下文指导顺序刷题
https://blog.csdn.net/linhuanmars/article/details/39546025
LeetCode 98
bool isValidBST(TreeNode* root) {
if(!root)
return true;
vector<int> arr;
inorder(root,arr);
for(int i=0;i<arr.size()-1;i++){
if(arr[i]>=arr[i+1]){
return false;
}
}
return true;
}
void inorder(TreeNode* root,vector<int> &arr){
if(root){
inorder(root->left,arr);
arr.push_back(root->val);
inorder(root->right,arr);
}
}
使用中序遍历,得到数组arr,如果arr是有序的,则说明该树一定是一颗二叉搜索树
LeetCode 99
这一题是考察思路,见过就能秒解,没见过,就很费事
中序遍历结果一定是有序的,如果出现一对元素交换可能会出现以下两种情况
1.只有一对逆序对,这个时候一定是相邻元素相交换
2.存在两对逆序对,这个时候一定是不相邻元素相交换
void recoverTree(TreeNode* root) {
vector<int> arr;
inorder(root,arr);
int x=-1,y=-1;
for(int i=0;i<arr.size()-1;i++){
if(arr[i]>arr[i+1]){
y=arr[i+1];
if(x==-1)
x=arr[i];
else
break;
}
}
recover(root,x,y);
}
void inorder(TreeNode *root,vector<int> &arr){
if(root){
inorder(root->left,arr);
arr.push_back(root->val);
inorder(root->right,arr);
}
}
void recover(TreeNode *root,int x,int y){
if(root){
recover(root->left,x,y);
if(root->val==x)
root->val=y;
else if(root->val==y)
root->val=x;
recover(root->right,x,y);
}
}
其中,如何在一个存在一对元素交换的有序数组中查找该逆序元素有点困难,这个地方的技巧是
(1)如果是中序遍历相邻的两个元素被调换了,很容易想到就只需会出现一次违反情况,只需要把这个两个节点记录下来最后调换值就可以;
(2)如果是不相邻的两个元素被调换了,会发生两次逆序的情况,那么这时候需要调换的元素应该是第一次逆序前面的元素,和第二次逆序后面的元素。
LeetCode 96
考察公式推导以及对二叉树理解
这其实是一个卡特兰数的模型,所以按照公式进行实现就可以
A [ n ] = ∑ i = 1 n − 1 A [ i ] ∗ A [ n − 1 − i ] A[n]=\sum_{i=1}^{n-1}A[i]*A[n-1-i] A[n]=i=1∑n−1A[i]∗A[n−1−i]
int numTrees(int n) {
vector<int> arr(n+1,0);
arr[0]=1;
arr[1]=1;
for(int i=2;i<=n;i++)
{
int temp=0;
for(int j=0;j<=i-1;j++){
temp+=arr[j]*arr[i-1-j];
}
arr[i]=temp;
}
return arr[n];
}
LeetCode 95
这题考察二叉搜索树,首先理解一下什么是二叉搜索(排序)树,二叉搜索树可以是一颗空树;若它的左子树不空,则左子树上所有结点的值均小于它的根结点的值; 若它的右子树不空,则右子树上所有结点的值均大于它的根结点的值; 它的左、右子树也分别为二叉排序树
其次该题是二叉树的构造,一般就是递归,就是分治法,看一下大神的代码
vector<TreeNode*> generateTrees(int n) {
if (n == 0) return {};
return helper(1, n);
}
vector<TreeNode*> helper(int start, int end) {
if (start > end) return {nullptr};
vector<TreeNode*> res;
for (int i = start; i <= end; ++i) {
auto left = helper(start, i - 1), right = helper(i + 1, end);
for (auto a : left) {
for (auto b : right) {
TreeNode *node = new TreeNode(i);
node->left = a;
node->right = b;
res.push_back(node);
}
}
}
return res;
}
对于分,自己其实可以理解,但是对于治自己不是很理解
LeetCode 108
在二叉树的构造篇有写
def sortedArrayToBST(self, nums):
"""
:type nums: List[int]
:rtype: TreeNode
"""
if not nums:
return None
mid=len(nums)/2
root=TreeNode(nums[mid])
root.left=self.sortedArrayToBST(nums[:mid])
root.right=self.sortedArrayToBST(nums[mid+1:])
return root
LeetCode 109
在二叉树的构造篇有写
TreeNode* sortedListToBST(ListNode* head) {
int n=0;
ListNode *p=head;
while(p!=NULL){
p=p->next;
n++;
}
if(n==0)
return NULL;
return build(head,n);
}
TreeNode* build(ListNode* head,int n){
int i=0;
ListNode *cur=head;
while(i<n/2){
i++;
cur=cur->next;
}
//Allocates size bytes of storage, suitably aligned to represent any object of that size, and returns a non-null pointer to the first byte of this block.
TreeNode *cur_=new TreeNode(cur->val);
if(n/2>0)
cur_->left=build(head,n/2);
else
cur_->left=NULL;
if((n-n/2-1)>0)
cur_->right=build(cur->next,n-n/2-1);
else
cur_->right=NULL;
return cur_;
}