剑指offer 27~38

27. 二叉树的镜像
class Solution {
public:
   //递归全自动
    TreeNode* mirrorTree(TreeNode* root) {
    if(root==nullptr)  return nullptr;
    TreeNode* temp = root->left;
    root->left = mirrorTree(root->right);
    root->right = mirrorTree(temp);
    return root;
    }
};
28. 对称的二叉树
class Solution {
public:
   //树递归
    bool isSymmetric(TreeNode* root) {
    if(root==nullptr)  return true;
    return dfs(root->left,root->right);
    }
    bool dfs(TreeNode* left,TreeNode* right){
        if(left == nullptr && right ==nullptr) return true;
        if(left ==nullptr || right==nullptr)   return false;
        if(left->val != right->val)  return false;
        return dfs(left->left,right->right) && dfs(left->right,right->left);
    }
};
29. 顺时针打印矩阵
class Solution {
public:
   //找边界问题
    vector<int> spiralOrder(vector<vector<int>>& matrix) {
    vector<int> res;
    if(matrix.size()==0 || matrix[0].size()==0) return res;
    int m = matrix.size(),n=matrix[0].size();
    int max = m*n;
    int left = 0,right = matrix[0].size()-1,up = 0,down = matrix.size()-1,num=0;
    while(1){
        for(int i=left;i<=right;i++){    //左到右的这一行。
            res.push_back(matrix[up][i]);
            num++;
            if(num == max)  return res;
        }
        for(int i=up+1;i<=down-1;i++){  //从上到下的这一列
           res.push_back(matrix[i][right]);
           num++;
           if(num == max)  return res;
        }
        for(int i=right;i>=left;i--){  //从右到左的这一行
           res.push_back(matrix[down][i]);
           num++;
           if(num == max)  return res;
        }
        for(int i=down-1;i>=up+1;i--){  //从下到上的这一列
           res.push_back(matrix[i][left]);
           num++;
           if(num == max)  return res;
        }
        left++;
        right--;
        up++;
        down--;
    }
    return res;
    }
};
30. 包含min函数的栈
class MinStack {
public:
    /** initialize your data structure here. */
    //设置两个栈s1,s2,s1正常装,s2装小的。
    stack<int> s1;
    stack<int> s2;
    

    MinStack() {
     
    }
    
    void push(int x) {
       if(s1.empty()) s2.push(x);
       else {
           if(s2.top() >x) s2.push(x);
           else s2.push(s2.top());
       }
       s1.push(x);
    }
    
    void pop() {
          s1.pop();
          s2.pop();
    }
    
    int top() {
         return s1.top();
    }
    
    int min() {
        return s2.top();
    }
};
31. 栈的压入、弹出序列
class Solution {
public:
    bool validateStackSequences(vector<int>& pushed, vector<int>& popped) {
    stack<int> st;
    int index=0;
    for(int i:pushed){
        st.push(i);
        while(!st.empty() && st.top()==popped[index]){
            st.pop();
            index++;
        }
    }
    return st.empty();
    }
};
32 - I. 从上到下打印二叉树
class Solution {
public:
    //层序遍历以队列为辅助空间
    vector<int> levelOrder(TreeNode* root) {
    queue<TreeNode*> queue;
    vector<int> res;

    if(!root)  return res;
    queue.push(root);

    while(!queue.empty()){
        for(int i=0;i<queue.size();i++){
            TreeNode* temp;
            temp = queue.front();
            res.push_back(temp->val);
            queue.pop();
            if(temp->left)  queue.push(temp->left);
            if(temp->right)  queue.push(temp->right);
        } 
    }
      return res;
    }
    
};
32 - II. 从上到下打印二叉树 II
class Solution {
public:
    vector<vector<int>> levelOrder(TreeNode* root) {
        vector<vector<int>>res;
        if(root==NULL)return res;
        queue<TreeNode*>q;
        q.push(root);
        while(!q.empty()){
            vector<int>r;
            int l=q.size();
            for(int i=0;i<l;i++){
                TreeNode* t=q.front();
                r.push_back(t->val);
                q.pop();
                if(t->left)q.push(t->left);
                if(t->right)q.push(t->right);
            }
            res.push_back(r);
        }
        return res;
    }
};
32 - III. 从上到下打印二叉树 III
/**
 * 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:
    //这次是从右取,把压栈的方式改一下就好了
 vector<vector<int>> levelOrder(TreeNode* root) {
        vector<vector<int>>res;
        if(root==NULL)return res;
        queue<TreeNode*>q;
        q.push(root);
        bool flag = 1;
        while(!q.empty()){
            vector<int>r;
            int l=q.size();
            for(int i=0;i<l;i++){
                TreeNode* t=q.front();
                r.push_back(t->val);
                q.pop();
                if(t->left)q.push(t->left);
                 if(t->right)q.push(t->right);
            }
            if(!flag)  res.push_back(vector<int>(r.rbegin(),r.rend()));
            else res.push_back(r);
            flag = !flag;
        }
        return res;
    }
};
Offer 33. 二叉搜索树的后序遍历序列
class Solution {
public:
   //书上写的是递归,后序遍历的最后一个为根节点,前面又连续n小于root,其余n个大于root。
    bool verifyPostorder(vector<int>& postorder) {
       if(postorder.size()<2)  return true;
       return dfs(postorder,0,postorder.size()-1);
    }

    bool  dfs(vector<int>& postorder,int left,int right){
        if(left>=right)  return true;

        int root = postorder[right];
        int k =left;
        while(postorder[k]<root)  k++;
        for(int i=k;i<right;i++)  if(postorder[i]<root)  return false;
        return dfs(postorder,left,k-1)&&dfs(postorder,k,right-1);
    }
};
34. 二叉树中和为某一值的路径
/**
 * 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:
    vector<vector<int>> pathSum(TreeNode* root, int sum) {
        vector<vector<int>> ret;
        vector<int> temp;
        dfs(temp, root, sum, ret);
        return ret;
    }
    void dfs(vector<int>& cur, TreeNode* node, int target,  vector<vector<int>>& ret)
    {
        if(!node)return;
        cur.push_back(node->val);
        target -= node->val;
        if(!target && !node->left && !node->right)ret.push_back(cur);
        
        dfs(cur, node->left, target, ret);
        dfs(cur, node->right, target, ret);
        cur.pop_back();
    }
};
35. 复杂链表的复制
class Solution {
public:
   //返回的是一个一模一样的链表。。
    Node* copyRandomList(Node* head) {
        //  return head;   这个竟然错了????
     if(!head)  return nullptr;
     Node* p = head;
     unordered_map<Node*,Node*> map;
     while(p){
         map[p] = new Node(p->val);
         p = p->next;
     }
     p = head;
     while(p){
         map[p]->next = p->next?map[p->next]:NULL;
         map[p]->random = p->random?map[p->random]:NULL;
         p = p->next;
     }
     return map[head];
    }
};
36. 二叉搜索树与双向链表
class Solution {
public:
    //中序遍历成为循环双向链表,原地转向.
    vector<Node*> temd;

    void dfs(Node* root){   //中序遍历
        if(!root) return;
        dfs(root->left);
        temd.push_back(root);
        dfs(root->right);
    }

    Node* treeToDoublyList(Node* root) {
     if(!root) return nullptr;
     dfs(root);
     for(int i=0;i<temd.size()-1;i++){
         temd[i]->right = temd[i+1];
         temd[i+1]->left = temd[i];
     }
     temd[0]->left = temd[temd.size()-1];
     temd[temd.size()-1]->right = temd[0];
     return temd[0];
    }
};
37. 序列化二叉树 丢你个嗨写不出来,没思路~
在这里插入代码片
38. 字符串的排列
class Solution {
public:
    //递归来写
    vector<string> permutation(string s) {
    vector<string> res;
    sort(s.begin(),s.end());
    dfs(res,"",s);
    return res;
    }
    void dfs(vector<string> &res,string temp,string s){
        if(temp.size() == s.size())  {res.push_back(temp);return;}
        for(int i=0;i<s.size();++i){
            if(s[i]=='*'||(i>0&&s[i]==s[i-1]))  continue;
            char a = s[i];
            temp = temp+s[i];
            s[i] = '*';  //标记
            dfs(res,temp,s);
            s[i] = a;
            temp.pop_back();
        }
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值