当前位置:网站首页>Leetcode - 7 - (nearest common ancestor of binary tree, rotation array, direct of binary tree, next permutation, combined sum)
Leetcode - 7 - (nearest common ancestor of binary tree, rotation array, direct of binary tree, next permutation, combined sum)
2022-04-22 07:26:00 【S atur】

class Solution {
public:
TreeNode* ans;
bool dfs(TreeNode* root, TreeNode* p, TreeNode* q){
if(root==NULL) return 0;
bool lson = dfs(root->left, p, q);
bool rson = dfs(root->right, p, q);
if((lson&&rson)||((root->val==p->val||root->val==q->val)&&(lson||rson))) ans = root;
return lson||rson||(root->val==p->val||root->val==q->val);
}
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
dfs(root, p, q); // Traversing down from a root node can encounter pq Two nodes
return ans;
}
};

class Solution {
public:
void rotate(vector<int>& nums, int k) {
int n = nums.size();
k %= n;
reverse(nums.begin(), nums.begin()+n);
reverse(nums.begin(), nums.begin()+k);
reverse(nums.begin()+k, nums.begin()+n);
}
};

class Solution {
public:
int ans;
int depth(TreeNode* root){
if(root==nullptr) return 0;
int l = depth(root->left);
int r = depth(root->right);
ans = max(ans, l+r+1);
return max(l, r)+1;
}
int diameterOfBinaryTree(TreeNode* root) {
ans = 1;
depth(root);
return ans-1;
}
};

class Solution {
public:
void nextPermutation(vector<int>& nums) {
int i = nums.size()-2;
while(i>=0&&nums[i]>=nums[i+1]) i --;
if(i>=0){
int j = nums.size()-1;
while(j>=0&&nums[i]>=nums[j]) j --;
swap(nums[i], nums[j]);
}
reverse(nums.begin()+i+1, nums.end());
}
};

class Solution {
public:
vector<vector<int>> ans;
vector<int> res;
void dfs(vector<int>& candidates, int target, int idx){
if(candidates.size()==idx) return;
if(target==0){
ans.push_back(res);
return;
}
dfs(candidates, target, idx+1); // Just skip
if(target-candidates[idx]>=0){
res.push_back(candidates[idx]); // Select the current value
dfs(candidates, target-candidates[idx], idx);
res.pop_back();
}
}
vector<vector<int>> combinationSum(vector<int>& candidates, int target) {
dfs(candidates, target, 0);
return ans;
}
};
版权声明
本文为[S atur]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220616059586.html
边栏推荐
- 【数论】同余(一):同余的基本概念与性质
- What is socket programming?
- Suppose there is such a relationship between the weight and height of adults: height (CM) - 100 = standard weight (kg). If the difference between a person's weight and its standard weight is between p
- 【题解】洛谷P6186 [NOI Online #1 提高组] 冒泡排序:【冒泡排序】与【逆序对】问题
- 小游戏——三子棋
- Vivado generates and invokes EDF netlist files
- (1) Download and installation of SQL Server
- SQL Server quick start
- Binary tree chain structure operation leetcode + Niuke (detailed explanation)
- CF1547E Air Conditioners
猜你喜欢

L2-001 紧急救援 (最短路Dijkstra的扩展 - 最短路径数&路径最大权值)

Anaconda installation and use

顺序表 增删查(找)

What is the internal structure of stack frame?

Virtual machine disk space shrinks

Cannot find interface mapping after updating HDF

(2) Basic configuration of SQL server and connection to SQL server using Navicat

Beyond compare solution to "authorization key has been revoked"

Host cannot Ping virtual machine in bridging mode

更新hdf之后无法找到接口映射
随机推荐
119 · 编辑距离
[Opt 31-67] Problem axi_ Interconnect RTL error
最强数组学习之路
【数论】素数(三):素数判断法(朴素法、模6法、Rabin-Miller及改进)
Relationship between A5 transceiver signal VOD and pre emphasis adjustment
(2) Basic configuration of SQL server and connection to SQL server using Navicat
14 lines of code to complete arbitrary selection of image crawling
[number theory] prime number (4): decomposition of numbers (Pollard rho)
843 · 数字翻转
Detailed tree array template -- Theory and code implementation
The thought and code of eight sorting
CF1547E Air Conditioners
Byte Summer Internship - 20220304
Codeforces Round #776 (Div. 3)
LeetCode - 4 - (接雨水、无重复字符的最长子串、分发糖果、二叉树的<前中后层>序遍历)
[DRC RTSTAT-1] Unrouted nets: 1 net(s) are unrouted
Define the class shape as the parent class, and define the method to calculate the perimeter and area in the class; (2) Define the shape subclass circle, with radius attribute and constant PI, and ove
762 · 最长公共子序列 II
When latex uses the template, the caption title of the picture cannot be left aligned
1420 · 最小覆盖子串II