编程积累5 递归 树

164 阅读2分钟

1.组合

又一个递归回溯法
class Solution {
private:
vector<vector<int>> ret={};
vector<int> tem={};
public:
    vector<vector<int>> combine(int n, int k) {
        
        if(n<=0 || k<=0)
             return ret;
        Auxcombine(n,k,1,tem);
        return ret;
        
    }
void Auxcombine(int n,int k,int j,vector<int> tem)
{
    if(tem.size()==k)
    {
        ret.push_back(tem);
        return;
    }
    else if(tem.size()!=k && j>n)
         return;
    else
    {
      for(int i=j;i<=n;i++)
     {
        tem.push_back(i);
        Auxcombine(n,k,i+1,tem);
        tem.pop_back();
     }
    }
}
};

2.给定一组不含重复元素的整数数组 nums,返回该数组所有可能的子集(幂集)。

说明:解集不能包含重复的子集。

class Solution {
private:
vector<int> tem={};
vector<vector<int>> ret={};
public:
    vector<vector<int>> subsets(vector<int>& nums) {
        if(nums.empty())
            return ret;
        ret.push_back(tem);
        int size=nums.size();
      for(int j=1;j<=size;j++)
      {
          Auxsubset(nums,size,0,j);
      }  
      return ret;
    }
    void Auxsubset(vector<int>& nums,int size,int at,int st)
    {
        if(tem.size()==st)
        {
             ret.push_back(tem);
             return;
        }
        else
        {
            for(int i=at;i<size;i++)
            {
                tem.push_back(nums[i]);
                Auxsubset(nums,size,i+1,st);
                tem.pop_back();
            }
        }
    }
};

3.分隔链表 给定一个链表和一个特定值 x,对链表进行分隔,使得所有小于 x 的节点都在大于或等于 x 的节点之前。

你应当保留两个分区中每个节点的初始相对位置。

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* partition(ListNode* head, int x) {
        ListNode* ret=NULL;
        if(head==NULL)
           return NULL;
        ListNode* litter=new ListNode();
        ListNode* litter_tem=litter;
        ListNode* bigger=new ListNode();
        ListNode* bigger_tem=bigger;
        ListNode* node=head;
        while(node!=NULL)
        {
            if(node->val<x)
               {
                   ListNode* tem=new ListNode(node->val);
                   litter_tem->next=tem;
                   litter_tem=tem;
                   node=node->next;
               }
            else if(node->val>=x)
            {
               ListNode* tem=new ListNode(node->val);
                bigger_tem->next=tem;
                bigger_tem=tem; 
                node=node->next;
            }
        }
        litter_tem->next=bigger->next;
        delete bigger;
        bigger=NULL;
        return litter->next;
        
    } 
};

4.验证二叉搜索树 给定一个二叉树,判断其是否是一个有效的二叉搜索树。

假设一个二叉搜索树具有如下特征:

节点的左子树只包含小于当前节点的数。
节点的右子树只包含大于当前节点的数。
所有左子树和右子树自身必须也是二叉搜索树。
class Solution {
public:
    bool isValidBST(TreeNode* root) {
       bool ret=aux_isValidBST(root,LONG_MIN,LONG_MAX);
       return ret;
        
    }
    bool aux_isValidBST(TreeNode* root,long long min,long long max)
    {
        if(root==NULL)
         return true;
         if(root->val<=min || root->val>=max)
           return false;
        return(aux_isValidBST(root->left,min,root->val)&&
               aux_isValidBST(root->right,root->val,max));
    }
};
5.对称二叉树

class Solution {
public:
    bool isSymmetric(TreeNode* root) {
        if(root==NULL)
          return true;
        return auxisSym(root->left,root->right);
        
    }
    bool auxisSym(TreeNode* p,TreeNode* q)
    {
       if(p==NULL && q==NULL)
          return true;
     if(p!=NULL && q!=NULL && p->val == q->val)
      return auxisSym(p->left,q->right)&&auxisSym(p->right,q->left);
      else
       return false;   
    }
};

6.二叉树的层序遍历

class Solution {
public:
    vector<vector<int>> levelOrder(TreeNode* root) {
        vector<vector<int>> a={};
        if (root==NULL)
           return a;
        queue<TreeNode*> qr={};
        qr.push(root);
        int len=qr.size();
        while(!qr.empty())
        {
            vector<int> tem;
            for(int i=0;i<len;i++)
            { 
              TreeNode* node=qr.front(); 
              tem.push_back(node->val);
              if(node->left)
                qr.push(node->left);
              if(node->right)
                qr.push(node->right);
              qr.pop();
            }
            len=qr.size();
            a.push_back(tem);
        }
        return a;
    }
};

7.二叉树的锯齿形层次遍历

    vector<vector<int>> zigzagLevelOrder(TreeNode* root) {
        vector<vector<int>> ret={};
        if(root==NULL)
          return ret;
        vector<TreeNode*> ve={};
        ve.push_back(root);
        int fz=1;
        int len=ve.size();
        while(!ve.empty())
        {
            vector<int> tem;
            if(fz==1)
            {
                for(int i=0;i<len;i++)
                {
                    TreeNode* node=ve.front();
                     ve.erase(ve.begin(),ve.begin()+1);
                    tem.push_back(node->val);
                     if(node->left)
                       ve.push_back(node->left);
                    if(node->right)
                       ve.push_back(node->right);  
                }
            }
            else if(fz==-1)
            {
               for(int i=0;i<len;i++)
                {
                    TreeNode* node=ve.back();
                    ve.pop_back();
                    tem.push_back(node->val);
                    if(node->right)
                       ve.insert(ve.begin(),node->right);
                    if(node->left)
                       ve.insert(ve.begin(),node->left);
                } 
            }
            ret.push_back(tem);
            len=ve.size();
            fz=-1*fz;
        }
        return ret;  
    }

8.二叉树的最大深度

    int maxDepth(TreeNode* root) {
        if(root==NULL)
          return 0;
        return Aux_maxDepth(root,0);
    }
    int Aux_maxDepth(TreeNode* root,int n)
    {
        if(root==NULL)
          return n;
        else 
        {
          int n1= Aux_maxDepth(root->left,n+1);
          int n2=Aux_maxDepth(root->right,n+1);
          n=n1>n2 ? n1 : n2;
        }
        return n;
    }

9.将有序数组转换为二叉搜索树

  TreeNode* sortedArrayToBST(vector<int>& nums) {
        if(nums.empty())
          return NULL;
        return Aux_sortefArrayToBst(nums,0,nums.size()-1);  
    }
    TreeNode* Aux_sortefArrayToBst(vector<int>& nums,int left,int right)
    {
        if(left>right)
          return NULL;
        
        int mid=(left+right)/2;
        TreeNode* node=new TreeNode(nums[mid]);
        node->left=Aux_sortefArrayToBst(nums,left,mid-1);
        node->right=Aux_sortefArrayToBst(nums,mid+1,right);
        return node;
    }

10.二叉树的最小深度

class Solution {
public:
   int minDepth(TreeNode* root) {
       if(root==NULL) return 0;
       else if(!root->left || !root->right){
           return 1+max(minDepth(root->left),minDepth(root->right));
       }
       else{
           return 1+min(minDepth(root->left),minDepth(root->right));
       }
   }
};