头像

Trevo


访客:2010

离线:13小时前


活动打卡代码 LeetCode 49. Group Anagrams

Trevo
4天前
//这里填你的代码^^
//注意代码要放在两组三个点之间,才可以正确显示代码高亮哦~



Trevo
4天前
//这里填你的代码^^
//注意代码要放在两组三个点之间,才可以正确显示代码高亮哦~

00




Trevo
4天前
//这里填你的代码^^
//注意代码要放在两组三个点之间,才可以正确显示代码高亮哦~



Trevo
18天前
//这里填你的代码^^
//注意代码要放在两组三个点之间,才可以正确显示代码高亮哦~

/
* 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:

unordered_map<int, int> pos;
TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {
    int n = preorder.size();
    //用哈希表(unordered_map<int,int>)记录每个值在中序遍历中的位置
    for(int i = 0; i < n; i++)
        pos[inorder[i]] = i;
    return dfs(preorder, inorder, 0, n - 1, 0, n - 1);
}
TreeNode* dfs(vector<int>& pre, vector<int>& inor, int pl, int pr, int il, int ir)
{
    if(pl > pr) return nullptr;
    //在中序遍历中找到根节点的位置 k
    int k = pos[pre[pl]] - il;
    TreeNode* root = new TreeNode(pre[pl]);
    root -> left = dfs(pre, inor, pl + 1, pl + k, il, il + k - 1);
    root -> right = dfs(pre, inor, pl + k + 1, pr, il + k + 1, ir);
    return root;
}

};




Trevo
18天前
//这里填你的代码^^
//注意代码要放在两组三个点之间,才可以正确显示代码高亮哦~

/
* 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:

unordered_map<int, int> pos;
TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {
    int n = preorder.size();
    //用哈希表(unordered_map<int,int>)记录每个值在中序遍历中的位置
    for(int i = 0; i < n; i++)
        pos[inorder[i]] = i;
    return dfs(preorder, inorder, 0, n - 1, 0, n - 1);
}
TreeNode* dfs(vector<int>& pre, vector<int>& inor, int pl, int pr, int il, int ir)
{
    if(pl > pr) return nullptr;
    //在中序遍历中找到根节点的位置 k
    int k = pos[pre[pl]] - il;
    TreeNode* root = new TreeNode(pre[pl]);
    root -> left = dfs(pre, inor, pl + 1, pl + k, il, il + k - 1);
    root -> right = dfs(pre, inor, pl + k + 1, pr, il + k + 1, ir);
    return root;
}

};




Trevo
20天前
//这里填你的代码^^
//注意代码要放在两组三个点之间,才可以正确显示代码高亮哦~

/
* 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 ans = 1;
int diameterOfBinaryTree(TreeNode
root) {
dfs(root);
return ans - 1;
}
int dfs(TreeNode* root)
{
if(!root) return 0;
int l = dfs(root -> left);
int r = dfs(root -> right);
ans = max(ans, l + r + 1);
return max(l, r) + 1;
}
};



活动打卡代码 LeetCode 101. Symmetric Tree

Trevo
20天前
//这里填你的代码^^
//注意代码要放在两组三个点之间,才可以正确显示代码高亮哦~

/
* 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:
bool isSymmetric(TreeNode
root) {
if(!root) return true;
queue[HTML_REMOVED] q;
q.push(root -> left);
q.push(root -> right);
while(!q.empty())
{
auto l = q.front();
q.pop();
auto r = q.front();
q.pop();
if(!l && !r) continue;
if(!l || !r) return false;
if(l -> val != r -> val) return false;

        q.push(l -> left);
        q.push(r -> right);
        q.push(l -> right);
        q.push(r -> left);
    }
    return true;
}

};




Trevo
20天前
//这里填你的代码^^
//注意代码要放在两组三个点之间,才可以正确显示代码高亮哦~

/
* 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) {
return root ? 1 + max(maxDepth(root -> left), maxDepth(root -> right)) : 0;
}
};



活动打卡代码 AcWing 35. 反转链表

Trevo
24天前
//这里填你的代码^^
//注意代码要放在两组三个点之间,才可以正确显示代码高亮哦~

/
* 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 new_head = nullptr;
while(head)
{
ListNode
next = head -> next;
head -> next = new_head;
new_head = head;
head = next;
}
return new_head;
}
};




Trevo
24天前
//这里填你的代码^^
//注意代码要放在两组三个点之间,才可以正确显示代码高亮哦~

class MinStack {
public:
stack[HTML_REMOVED] st;
stack[HTML_REMOVED] minSt;
/* initialize your data structure here. /
MinStack() {

}

void push(int x) {
    if(minSt.empty() || x < minSt.top()) 
    {
        minSt.push(x);
    }
    st.push(x);
}

void pop() {
    int x = st.top();
    if(minSt.top() == x)
        minSt.pop();
    st.pop();
}

int top() {
    if(st.size()) return st.top();
}

int getMin() {
    if(minSt.size()) return minSt.top();
    else return -1;
}

};

/*
* Your MinStack object will be instantiated and called as such:
* MinStack obj = new MinStack();
* obj.push(x);
* obj.pop();
* int param_3 = obj.top();
* int param_4 = obj.getMin();
/