《九日集训》第十五轮 (第九讲) 简单递归
知识点
递归
int JieCheng(int n) {
if(n <= 1) {
return 1;
}
return n * JieCheng(n-1);
}
题目分析
题目1
分析
本质上是统计5的个数有多少个
代码
class Solution {
public:
vector<int> shuffle(vector<int>& nums, int n) {
vector<int>result;
for(int i=0;i<n;i++){
result.push_back(nums[i]);
result.push_back(nums[i+n]);
}
return result;
}
};
题目2
分析
简单的模拟而已
代码
class Solution {
public:
int numberOfSteps(int num) {
int res=0;
while(num!=0){
if(num%2==0){
num/=2;
res++;
}else{
num--;
res++;
}
}
return res;
}
};
题目3
分析
代码随想录刷过这题,利用完全二叉树的性质来做。如果是满二叉树,节点数直接为 2^树深度 - 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:
int res=0;
int countNodes(TreeNode* root) {
if(root==nullptr)return 0;
TreeNode*left=root->left;
TreeNode*right=root->right;
int leftHight=0,rightHight=0;
while(left){//左子树深度
left=left->left;
leftHight++;
}
while(right){//右子树深度
right=right->right;
rightHight++;
}
if(leftHight==rightHight)return (2<<leftHight)-1;
return countNodes(root->left)+countNodes(root->right)+1;
}
};
题目4
分析
树的遍历,把结果存到set计算set的长度即是答案
代码
/**
* 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:
set<int>res;
int numColor(TreeNode* root) {
if(root){
numColor(root->left);
numColor(root->right);
res.insert(root->val);
}
return res.size();
}
};
题目5
分析
灵感来自于104. 二叉树的最大深度 ,由
- 如果 \(n\)是偶数,则用 \(\frac{n}{2}\)替换 \(n\) 。
- 如果 \(n\)是奇数,则可以用 \(n + 1\)或\(n - 1\)替换\(n\) 。
可以直接写出递归表达式
代码
class Solution {
public:
int integerReplacement(int n) {
if(n==1)return 0;
if(n==2147483647)return 32;
if(n%2==0)return integerReplacement(n/2)+1;
else return min(integerReplacement(n+1),integerReplacement(n-1))+1;
}
};
题目6
分析
将二叉树遍历一下,当结点的值位于区间时就相加
答案
/**
* 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 sum=0;
int rangeSumBST(TreeNode* root, int low, int high) {
if(root==nullptr)return 0;
if(root->val>=low && root->val<=high)sum+=root->val;
rangeSumBST(root->left,low,high);
rangeSumBST(root->right,low,high);
return sum;
}
};
题目7
分析
和104. 二叉树的最大深度 一毛一样
代码
/**
* 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:
int maxDepth(TreeNode* root) {
if(root==nullptr)return 0;
return max(maxDepth(root->left),maxDepth(root->right))+1;
}
};
题目8
分析
代码随想录上刷过这题qwq,整体最大深度其实是左子树和右子树中的最大的那个深度
代码
/**
* 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 maxDepth(TreeNode* root) {
if(root==nullptr)return 0;
return max(maxDepth(root->left),maxDepth(root->right))+1;
}
};
题目9
分析
代码随想录上刷过,把每一个节点的左右子树交换一下就可以了。
代码
/**
* 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* invertTree(TreeNode* root) {
if(root==nullptr)return root;
swap(root->right,root->left);
invertTree(root->left);
invertTree(root->right);
return root;
}
};
题目10
分析
直接dfs一下即可
代码
class Solution {
public:
int n;
vector<vector<int>> res;//结果
vector<int>road;//路径
void dfs(int u,vector<vector<int>>& graph){
road.push_back(u);
if(u==n-1)res.push_back(road);
for(auto x:graph[u])dfs(x,graph);
//回溯
road.pop_back();
}
vector<vector<int>> allPathsSourceTarget(vector<vector<int>>& graph) {
n=graph.size();
dfs(0,graph);
return res;
}
};
题目11
分析
和797. 所有可能的路径一毛一样
代码
class Solution {
public:
int n;
vector<vector<int>> res;//结果
vector<int>road;//路径
void dfs(int u,vector<vector<int>>& graph){
road.push_back(u);
if(u==n-1)res.push_back(road);
for(auto x:graph[u])dfs(x,graph);
//回溯
road.pop_back();
}
vector<vector<int>> allPathsSourceTarget(vector<vector<int>>& graph) {
n=graph.size();
dfs(0,graph);
return res;
}
};
总结
今天的题目偏简单qwq


浙公网安备 33010602011771号