Commit d00e3a5a authored by yaozc's avatar yaozc
Browse files

new era

parent c10737d8
......@@ -313,5 +313,179 @@ public:
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
// solution1: recursive
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* mergeKLists(vector<ListNode*>& lists) {
if(lists.empty()){
return nullptr;
}
while (lists.size() > 1) {
lists.push_back(merge_two_linked_lists(lists[0], lists[1]));
lists.erase(lists.begin(), ++++lists.begin());
}
return lists[0];
}
ListNode* merge_two_linked_lists(ListNode* l1, ListNode* l2){
if (!l1) return l2;
if (!l2) return l1;
if (l1->val < l2->val) {
l1->next = merge_two_linked_lists(l1->next, l2);
return l1;
}
else{
l2->next = merge_two_linked_lists(l1, l2->next);
return l2;
}
}
};
//Reverse a linked list
// solution1: iteratively
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* reverseList(ListNode* head) {
ListNode* current = head;
ListNode* prev = NULL;
ListNode* next = NULL;
while(current){
next = current->next;
current->next = prev;
prev = current;
current = next;
}
return prev;
}
};
//solution2: recursively
class Solution {
public:
ListNode* reverseList(ListNode* head) {
if (!head) return nullptr;
if (!head->next) return head;
head->next = reverseList(head->next);
return head;
}
};
//今天是yorkevin账号购买LeetCode的第一天。新的开始!让我们我们在这里留下想法和心得!
// Validate Binary Search Tree
//每个点都应该在一个特定的range之内,具体大小为:root左边的第一个点在(负无穷到root->val)
// root右边的第一个点在(正无穷到root->val)
//每call一次recursion就update一个max和min
class Solution {
public:
bool isValidBST(TreeNode* root) {
return checker(root, LONG_MAX, LONG_MIN);
}
bool checker(TreeNode* root, long max, long min){
if(!root) return true;
else if(root->val >= max || root->val <= min) return false;
return checker(root->left, root->val, min) && checker(root->right, max, root->val);
}
};
// Symmetric Tree
// https://www.youtube.com/watch?v=XV7Sg2hJO3Q&t=613s
// 本质为check leftsub.left == rightsub.right && leftsub.right == rightsub.left
// 遇到这种问题首先想想是不是只要比较 == 一次就ok
class Solution {
public:
bool isSymmetric(TreeNode* root) {
if(!root) return true;
return helper(root->left, root->right);
}
bool helper(TreeNode* left, TreeNode* right){
if(!left && !right) return true;
else if(left && right){
return left->val == right->val && helper(left->left, right->right) && helper(left->right, right->left);
}
return false;
}
};
// Binary Tree Level Order Traversal
// nothing fancy,这道题主要考BFS,记住BFS广度搜索要用queue,DFS用stack
// 主要细节在与while中的for loop。不可以用while套while来代替!! 因为queue的size一直在变
// 所以每个level的大小都是有这个循环开始时 queue的大小来决定的!!!
// https://www.youtube.com/watch?v=gcR28Hc2TNQ
// https://www.youtube.com/watch?v=XZnWETlZZ14
class Solution {
public:
vector<vector<int>> levelOrder(TreeNode* root) {
vector<vector<int>> res;
queue<TreeNode*> q;
if(!root) return res;
q.push(root);
while(!q.empty()){
vector<int> temp;
int end=q.size();
for(int i=0; i<end; i++){
TreeNode* current = q.front();
q.pop();
temp.push_back(current->val);
if(current->left) q.push(current->left);
if(current->right) q.push(current->right);
}
res.push_back(temp);
}
return res;
}
};
//
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment