128. Longest Consecutive Sequence(*)
class Solution {
public:
int longestConsecutive(vector<int>& nums) {
unordered_set<int> set;
for(int num:nums){
set.insert(num);
}
int res = 0;
for(int val:set){
if(set.count(val-1)){
continue;
}
int curVal = val;
int curLen = 1;
while(set.count(curVal+1)){
curVal++;
curLen++;
}
res = max(res, curLen);
}
return res;
}
};
注意如何判断是不是连续数第一个数字
104. Maximum Depth of Binary Tree
1.动态规划方法
class Solution {
public:
int maxDepth(TreeNode* root) {
if(root == NULL) return 0;
int left = maxDepth(root->left);
int right = maxDepth(root->right);
return 1+max(left, right);
}
};
2.回溯
class Solution {
private:
int res = 0;
int depth = 0;
void traversal(TreeNode* root){
if(root == NULL) return;
depth++;
res = max(res, depth);
traversal(root->left);
traversal(root->right);
depth--;
}
public:
int maxDepth(TreeNode* root) {
traversal(root);
return res;
}
};
100. Same Tree
class Solution {
public:
bool isSameTree(TreeNode* p, TreeNode* q) {
if(p == NULL && q == NULL) return true;
if(p == NULL || q == NULL) return false;
if(p->val != q->val) return false;
return isSameTree(p->left, q->left) && isSameTree(p->right, q->right);
}
};
要明确终止条件
226. Invert Binary Tree
class Solution {
public:
TreeNode* invertTree(TreeNode* root) {
if(root == NULL) return NULL;
TreeNode* left = invertTree(root->left);
TreeNode* right = invertTree(root->right);
root->left = right;
root->right = left;
return root;
}
};
class Solution {
private:
void traversal(TreeNode* root){
if(root == NULL) return;
TreeNode* tmp = root->left;
root->left = root->right;
root->right = tmp;
traversal(root->left);
traversal(root->right);
}
public:
TreeNode* invertTree(TreeNode* root) {
traversal(root);
return root;
}
};
101. Symmetric Tree(想思路)
class Solution {
private:
bool compare(TreeNode* left, TreeNode* right){
if(left == NULL && right == NULL) return true;
if(left == NULL || right == NULL) return false;
if(left->val != right->val) return false;
return compare(left->left, right->right) && compare(left->right, right->left);
}
public:
bool isSymmetric(TreeNode* root) {
return compare(root->left, root->right);
}
};
199. Binary Tree Right Side View
class Solution {
private:
vector<int> res;
int depth = 0;
void traversal(TreeNode* root){
if(root == NULL) return;
if(depth == res.size()){
res.push_back(root->val);
}
depth++;
traversal(root->right);
traversal(root->left);
depth--;
}
public:
vector<int> rightSideView(TreeNode* root) {
traversal(root);
return res;
}
};
要记得回溯的时候depth-1
class Solution {
public:
vector<int> rightSideView(TreeNode* root) {
queue<TreeNode*> que;
vector<int> res;
if(root != NULL) que.push(root);
while(!que.empty()){
int size = que.size();
for(int i=0; i<size; i++){
TreeNode* cur = que.front();
if(i == 0) res.push_back(cur->val);
que.pop();
if(cur->right) que.push(cur->right);
if(cur->left) que.push(cur->left);
}
}
return res;
}
};
19. Remove Nth Node From End of List(想注意事项)
class Solution {
public:
ListNode* removeNthFromEnd(ListNode* head, int n) {
ListNode* dummy = new ListNode(0);
dummy->next = head;
ListNode* fast = dummy;
ListNode* slow = fast;
while(n>=0){
fast = fast->next;
n--;
}
while(fast != NULL){
slow = slow->next;
fast = fast->next;
}
slow->next = slow->next->next;
return dummy->next;
}
};
这道题要注意的就是要写虚拟表头
82. Remove Duplicates from Sorted List II
class Solution {
public:
ListNode* deleteDuplicates(ListNode* head) {
ListNode* dummy = new ListNode(0);
dummy->next = head;
ListNode* pre = dummy;
while(head != NULL){
if(head->next != NULL && head->next->val == head->val){
while(head->next != NULL && head->next->val == head->val){
head = head->next;
}
pre->next = head->next;
}else{
pre = pre->next;
}
head = head->next;
}
return dummy->next;
}
};
这里要改变链表就要写pre->next = head->next