力扣刷题记录1

类型总结:

数组 1538
字符串 683
哈希表 535
动态规划 502
数学 493
排序 351
深度优先搜索 328
贪心 314
广度优先搜索 263
树 261
二分查找 252
矩阵 223
数据库 220
二叉树 217
双指针 205
位运算 194
栈 165
堆(优先队列)158
设计 150
图 145
前缀和 133
模拟 126
回溯 120
计数 109
链表 102
滑动窗口 97
并查集 85
递归 63
有序集合 58
分治 58
单调栈 56
二叉搜索树 56
字典树 55
枚举 51
队列 48
状态压缩 43
记忆化搜素 41
几何 39
线段树 39
数论 37
拓扑排序 37
树状数组 30
哈希函数 27
博弈 25
数据流 24
最短路 22
组合数学 22
字符串匹配 21
滚动哈希 19
交互 18
脑筋急转弯 15
随机化 14
归并排序 14
单调队列 13
双向链表 12
快速选择 11
迭代器 10
概率与统计 9
多线程 9
桶排序 8
后缀数组 6
计数排序 6
最小生成树 5
扫描线 4
Shell 4
水塘抽样 4
欧拉回路 3
基数排序 3
强连通分量 2
双连通分量 2
拒绝采样2

  1. 统计同质子字符串的数目(数学)

数学题,长整形的使用

class Solution
{
public:
    int countHomogenous(string s)
    {
        long total = 0, size = s.size();
        long MOD = (long)(1e9 + 7);
        for (int i = 0; i < size;)
        {
            int j;
            for (j = i; j < size && s[i] == s[j]; j++)
                ;
            long cnt = j - i;
            total += (cnt + 1) * cnt / 2;
            total %= MOD;
            i = j;
        }
        return total;
    }
};

79- 单词搜索(数组)

数组题,熟练使用数组操作,这个题有个注意的地方就是要记录访问过的index。

class Solution {
private:
    int dx[4] = {-1, 0, 1, 0};
    int dy[4] = {0, -1, 0, 1};
    bool dfs(vector<vector<char>>& board, vector<vector<bool>>& isVisited, string word, int x, int y, int idx) {
        if(idx >= word.size()) {
            return true;
        }
        bool result = false;
        for(int i = 0; i < 4; i++) {
            int temX = x + dx[i];
            int temY = y + dy[i];
            if(temX < 0 || temX >= board.size()) continue;
            if(temY < 0 || temY >= board[0].size()) continue;
            if(!isVisited[temX][temY] && board[temX][temY] == word[idx]) {
                isVisited[temX][temY] = true;
                result = dfs(board, isVisited, word, temX, temY, idx + 1);
                isVisited[temX][temY] = false;
                if(result) break;
            }
        }
        return result;
    }
public:
    bool exist(vector<vector<char>>& board, string word) {
        int m = board.size();
        int n = board[0].size();

        int headCount = 0;
        int tailCount = 0;
        for(int i = 0; i < m; i++) {
            for(int j = 0; j < n; j++) {
                if(board[i][j] == word[0]) {
                    ++headCount;
                }
                else if(board[i][j] == word[word.size() - 1]) {
                    ++tailCount;
                }
            }
        }

        if(tailCount < headCount) reverse(word.begin(), word.end());

        vector<vector<bool>> isVisited(m, vector<bool>(n, false));
        for(int i = 0; i < m; i++) {
            for(int j = 0; j < n; j++) {
                if(board[i][j] == word[0]) {
                    isVisited[i][j] = true;
                    auto flag = dfs(board, isVisited, word, i, j, 1);
                    isVisited[i][j] = false;
                    if(flag) return flag;
                }
            }
        }
        return false;
    }
};
2441. 与对应负数同时存在的最大正整数

(巧用数组)

class Solution {
public:
    int findMaxK(vector<int>& nums) {
        int i, hash[2001]{}, maxValue = -1;
        for (int num: nums) {
            if (hash[1000 - num]) {
                maxValue = max(maxValue, abs(num));
            }
            hash[num + 1000]++;
        }
        return maxValue;
    }
};


  1. 祖父节点值为偶数的节点和(使用递归时,传入3个参数,分别代表孙,父,爷)
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    int ans = 0;
    void dfs(TreeNode* root,TreeNode* fa,TreeNode* ffa)
    {
        if(root == nullptr) return;
        if(ffa && ffa->val % 2 == 0)
        {
            ans += root->val;
        }
        dfs(root->left,root,fa);
        dfs(root->right,root,fa);
    }
    int sumEvenGrandparent(TreeNode* root) {
        dfs(root,nullptr,nullptr);
        return ans;
    }
};

506 相对名次

学会使用sort,内部可以是pair类型

class Solution {
public:
    vector<string> findRelativeRanks(vector<int>& score) {
        int n=score.size();
        string desc[3]={"Gold Medal","Silver Medal","Bronze Medal"};
        vector<pair<int,int>>arr;
        for(int i=0;i<n;i++)
        {
            arr.emplace_back(make_pair(-score[i],i));
        }
        sort(arr.begin(),arr.end());
        vector<string>ans(n);
        for(int i=0;i<n;i++)
        {
            if(i>=3)
                ans[arr[i].second]=to_string(i+1);
            else
                ans[arr[i].second]=desc[i];
        }
        return ans;
    }
};

704 二分查找

因为nums的数量有可能为1,或2,在循环时middle要+1或-1,是为了检测是否超过检索范围

class Solution {
public:
    int search(vector<int>& nums, int target) {
          int start = 0;
          int end = nums.size() - 1;;
          int middle = start + (end - start)/2;

          while(middle >= start && middle <= end){

              if(nums[middle] == target){
                return middle;
             }
             
             if(nums[middle] > target){
                 end = middle - 1;
                 middle = start + (end - start)/2;
             }
              if(nums[middle] < target){
                  start = middle + 1;
                  middle = start + (end - start)/2;
              }
         }
            return -1;
    }

};

27.移除元素

为了不使用多余的变量,当检测到val后与后面的元素互换。

class Solution {
public:
    int removeElement(vector<int>& nums, int val) {

        int count = 0;
        for(int i=0; i<nums.size();i++){
            
            if(nums.at(i) == val){
                
                for(int j=i+1; j<nums.size(); j++){
                    if(nums.at(j) != val){
                        count++;
                        nums.at(i) = nums.at(j);
                        nums.at(j) = val;
                        break;
                    }
                }
                
            }
            else{
                count++;
            }

        }

        return count;
    }
};

207课程表

这个是自己写的,虽然超时但是示例都通过,注意的是要考虑递归时不要进入死循环。

class Solution {
public:

    bool check(int a, int b, set<int>& visited, std::map<int,vector<int>>& data){
         
         if(data.find(a) != data.end()){

             if(visited.find(a) != visited.end()){
                 return true;
             }
             else{
                 visited.insert(a);
             }

             vector<int> set = data.at(a);
             for(auto it : set){
                
                if(it == b){
                    return false;
                }
                else{
                    if(check(it,b,visited,data) == false){
                        return false;
                    }

                    visited.insert(it);
                }
             }

             return true;             
         }
         else{
             return true;
         }
    }

    bool canFinish(int numCourses, vector<vector<int>>& prerequisites) {
          
          std::map<int,vector<int>> needs;

          for(auto data : prerequisites){
              int index = data.at(0);
              int content = data.at(1);

              if(needs.find(index) == needs.end()){
                    vector<int> need;
                    need.push_back(content);
                    needs.emplace(index,need);
              }
              else{
                  needs.at(index).push_back(content);
              }
          }

          for(int i=0; i<numCourses; i++){

             if(needs.find(i) == needs.end()){
                 continue;
             }

            for(auto it : needs.at(i)){

                 std::set<int> visited;

                 bool state = check(it,i,visited,needs);
                 if(state == false) return false;
            }
          }

          return true;
          }
};

209 长度最小子数组

容易出错的地方是在左边缩短的时候会漏掉循环

class Solution {
public:
    int minSubArrayLen(int target, vector<int>& nums) {

          //int left = 0;
          int right = 0;
          int sum = 0;
          int min_length = nums.size() + 1;

          std::queue<int> temp;

          while(right < nums.size()){
              
              temp.push(nums.at(right));
              sum += nums.at(right);

              while(sum >= target){
                  if(temp.size() < min_length){
                      min_length = temp.size();      
                  }
                  sum -= temp.front();
                  temp.pop();
                  //left++;
              }
              
              right++;
          }

          if(min_length <= nums.size()){
              return min_length;
          }
          else{
              return 0;
          }


    }
};

59.螺旋矩阵

访问过的点用一个二维矩阵保存起来

class Solution {
public:
    vector<vector<int>> generateMatrix(int n) {

        vector<vector<int>> all_data;

        for(int i=0; i<n; i++){
            vector<int> temp;
            temp.resize(n);
            all_data.push_back(temp);
        }

        int a[n][n];

        for(int i=0; i<n; i++){
            for(int j=0; j<n; j++){
                a[i][j] = 0;
            }
        }

        //0的方向向左,1的方向向下,2的方向向右,3的方向向上
        int way = 0;

        int i = 0;
        int j = 0;
        int value = 1;

        int count = 0;
        int number = 0;

        while(1){

            if(count == n*n){
                break;
            }

            number++;

             //向右
             if(way == 0){
                if(j<n && a[i][j] == 0){
                 all_data.at(i).at(j) = value;
                 value++;
                 a[i][j] = 1; //is visited;
                 j++;
                 count++;
                }
                else{
                    way = 1;
                    j--;
                    i++;
                }
             }

             //向下
             if(way == 1){

                 if(i<n && a[i][j] == 0){
                    all_data.at(i).at(j) = value;
                    value++;
                    a[i][j] = 1; //is visited;
                    i++;
                    count++;
                 }
                 else{
                     way = 2;
                     i--;
                     j--;
                 }

             }

             //向左
             if(way == 2){

                 if(j>=0 && a[i][j] == 0){
                    all_data.at(i).at(j) = value;
                    value++;
                    a[i][j] = 1; //is visited;
                    j--;
                    count++;
                 }
                 else{
                     way = 3;
                     j++;
                     i--;
                 }
             }

             //向上
             if(way == 3){
                 if(i>=0 && a[i][j] == 0){
                    all_data.at(i).at(j) = value;
                    value++;
                    a[i][j] = 1; //is visited;
                    i--;
                    count++;
                 }
                 else{
                     way = 0;
                     i++;
                     j++;
                 }
             }

        }

        return all_data;
    }
};

203.移除链表元素

虽然不是最优,但是是自己写的

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    ListNode* removeElements(ListNode* head, int val) {
            
        bool is_first = true;

        while(head !=nullptr){
            if(head->val == val){
                if(is_first == true){
                    head = head->next;
                }
            }
            else{
                break;
            }
        }

         if(head == nullptr) return head;

        ListNode* temp = head;
        while(temp->next != nullptr){

               if(temp->next->val == val){
                       temp->next = temp->next->next;
               }
               else{
                   temp = temp->next;
               }
        }

         return head;
    }
};

707 设计链表

自己一行行扣出来的,虽不是最优,留个纪念吧

class MyLinkedList {

  struct ListNode {
      int val;
      ListNode *next;
      ListNode() : val(0), next(nullptr) {}
      ListNode(int x) : val(x), next(nullptr) {}
      ListNode(int x, ListNode *next) : val(x), next(next) {}
  };


public:
    MyLinkedList() {
        head = nullptr;
    }
    
    int get(int index) {
       
        int count = 0;
        ListNode* result = head;
        int value = -1;

        while(result != nullptr){
            if(count == index){
                value = result->val;
                break;
            }
            else{
                count++;
                result = result->next;
            }
        }

        return value;
    }
    
    void addAtHead(int val) {
       
       ListNode* first = new ListNode(val);

       first->next = head;
       head = first;
       length++;
       return;
    }
    
    void addAtTail(int val) {

       ListNode* end = new ListNode(val);

       ListNode* result = head;
       if(result == nullptr)
       {
           head = end;
           length++;
           return;
       }

       while(result->next != nullptr){
           result = result->next;
       }

       result->next = end;
       length++;

       return;
    }
    
    void addAtIndex(int index, int val) {
      
        int count = 0;
        ListNode* result = head;

        if(length == index){
            addAtTail(val);
            return;
        }

        while(result != nullptr){

            if(index == 0){
                ListNode* add = new ListNode(val);
                add->next = result;
                head = add;
                length++;
                break;
            }

            if(count == index-1){
                ListNode* add = new ListNode(val);
                add->next = result->next;
                result->next = add;
                length++;
                break;
            }
            else{
                count++;
                result = result->next;
            }
        }
       return;
    }
    
    void deleteAtIndex(int index) {
        int count = 0;
        ListNode* result = head;

        if(index == 0){
            if(result != nullptr){
                head = result->next;
                length--;
                return;
            }
        }

        if(index >= length){
            return;
        }

        while(result != nullptr){

            if(count == index-1){
                result->next = result->next->next;
                length--;
                break;
            }
            else{
                count++;
                result = result->next;
            }
        }
       return;
    }

    private:
    ListNode* head;
    int length = 0;
};

/**
 * Your MyLinkedList object will be instantiated and called as such:
 * MyLinkedList* obj = new MyLinkedList();
 * int param_1 = obj->get(index);
 * obj->addAtHead(val);
 * obj->addAtTail(val);
 * obj->addAtIndex(index,val);
 * obj->deleteAtIndex(index);
 */

206 反转链表

自己写的,内存占用还更少

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    ListNode* reverseList(ListNode* head) {

        bool is_first = true;

        if(head == nullptr || head->next == nullptr){
            return head;
        }

        ListNode* a1 = head;
        ListNode* a2 = a1->next;

        while(a2 != nullptr){
            
            if(is_first == true){
                ListNode* temp = a2->next;

                a2->next = a1;
                a1->next = nullptr;

                a1 = a2;
                a2 = temp;

                is_first = false;
                
            }
            else{
                ListNode* temp = a2->next;

                a2->next = a1;

                a1 = a2;
                a2 = temp;
            }
        }

        head = a1;

        return head;
    }
};

19 删除链表的倒数第 N 个结点

占用了些内存,不是最优解

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    ListNode* removeNthFromEnd(ListNode* head, int n) {

             int length = 0;
             ListNode* result = head;

             while(result != nullptr){
                 length++;
                 result = result->next;
             }

            int r_index = length - n;

            if(r_index == 0){
                head = head->next;
                return head;
            }

            result = head;
            int index = 0;

            while(result != nullptr){
                
                index++;
                if(index == r_index){
                   result->next = result->next->next;
                   break;
                }
                else{
                    result = result->next;
                }
            }

            return head;
    }
};

142.环形链表

就是龟兔赛跑的算法,注意起点就是交点的情况

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode *detectCycle(ListNode *head) {
        
         if(head == nullptr){
             return nullptr;
         }

         if(head != nullptr && head->next == nullptr){
             return nullptr;
         }

         int index = 0;

         ListNode* start = head;

         ListNode* slow = head;
         ListNode* fast = head;

         while(1){
             if(slow == nullptr ){
                 return nullptr;
             }
             else{
                 slow = slow->next;
             }

            if(fast == nullptr ){
                 return nullptr;
             }
             else if(fast->next == nullptr ){
                 return nullptr;
             }
             else{
                 fast = fast->next->next;
             }

             if(slow == fast){
                  
                  if(slow == start){
                      return slow;
                  }
                  break;
             }
         }

         slow = start;
         while(1){
             slow = slow->next;
             fast = fast->next;

             if(slow == fast){
                 return slow;
             }
         }

    }
};

242.有效的字母异位词

自己写的,效率比较低

class Solution {
public:
    bool isAnagram(string s, string t) {

         std::map<char,int> all_data;

         for(int i=0; i< s.length(); i++){
             if(all_data.find(s[i]) == all_data.end()){
                 all_data.emplace(s[i],1);
             }
             else{
                 int value = all_data.at(s[i]);// + 1;
                 all_data.at(s[i]) = value + 1;
             }
         }

         for(int i=0; i<t.length(); i++){
             if(all_data.find(t[i]) == all_data.end()){
                 return false;
             }
             else{
                 int value = all_data.at(t[i]);// - 1;
                 all_data.at(t[i]) = value - 1;

                 if(value - 1 == 0){
                     all_data.erase(t[i]);
                 }
             }
         }

        if(all_data.size() == 0){
            return true;
        }else{
            return false;
        }



    }
};

349.两个数组的交集

使用unordered_set

class Solution {
public:
    vector<int> intersection(vector<int>& nums1, vector<int>& nums2) {
            
            std::set<int> data;

            vector<int> outcome;

            for(auto it : nums1){
                data.insert(it);
            } 

            std::set<int> result;
            for(auto it : nums2){
                if(data.find(it) != data.end()){
                    result.insert(it);
                }
            }

            for(auto it : result){
                outcome.push_back(it);
            }

            return outcome;

    }
};

1.两数之和

内存占用比较大,不管怎么说,自己写的

class Solution {
public:
    vector<int> twoSum(vector<int>& nums, int target) {
        
        std::unordered_map<int,int> map_data;
        vector<int> result;

        for(int i=0; i<nums.size(); i++){

            int value = nums[i];
            int other = target - value;

            if(map_data.find(other) != map_data.end()){
                result.push_back(i);
                int index = map_data.at(other);
                result.push_back(index);
                break;
            }

            map_data.emplace(nums[i],i);
        }

        return result;

    }
};

454.四数相加

执行时间有些慢,自己写的

class Solution {
public:
    int fourSumCount(vector<int>& nums1, vector<int>& nums2, vector<int>& nums3, vector<int>& nums4) {

        std::unordered_map<int,int> map_12;

        for(int i=0; i<nums1.size(); i++){
            
            int value_1 = nums1[i];
            
            for(int j=0; j<nums2.size(); j++){

                 int value_2 = nums2[j];
                 int sum = value_1 + value_2;

                 if(map_12.find(sum) == map_12.end()){
                     map_12.emplace(sum,1);
                 }
                 else{
                     int count = map_12.at(sum);
                     map_12.at(sum) = count + 1;
                 }
            }
        }


      int sum_count = 0;

      for(int i=0; i<nums3.size(); i++){
            
            int value_1 = nums3[i];
            
            for(int j=0; j<nums4.size(); j++){

                 int value_2 = nums4[j];
                 int sum = value_1 + value_2;

                 int other_value = 0 - sum;

                 if(map_12.find(other_value) != map_12.end()){
                    
                     int other_count = map_12.at(other_value);
                     sum_count += other_count;
                 }
                 else{
                     continue;
                 }
            }
        }

        return sum_count;



    }
};

15.三数之和

自己写的代码,应该在一开始就进行排序

class Solution {
public:

    bool is_exist(vector<vector<int>>& result, vector<int>& data){
          
          for(int i=0; i<result.size(); i++){
               vector<int> check = result[i];

                //bool is_same = true;
                if(check == data){
                    return true;
                }

                // for(int j=0; j
                //     if(check[j] != data[j]){
                //         is_same == false;
                //         break;
                //     }
                // }

                // if(is_same == true){
                //     return true;
                // }
          }

          return false;
    }

    vector<vector<int>> threeSum(vector<int>& nums) {
       
       std::map<int,int> data;
       vector<vector<int>> result;

       for(int i=0; i< nums.size(); i++){
           data.emplace(nums[i],i);
       }

       for(int i=0; i<nums.size(); i++){
           
           int value_i = nums[i];

           for(int j=i+1; j< nums.size(); j++){
                
                int value_j = nums[j];

                int value_k = 0 - (value_i + value_j);

                if(data.find(value_k) != data.end()){
                     
                     int index_k = data.at(value_k);

                     if(index_k != i && index_k != j){
                         vector<int> temp;
                         temp.push_back(value_i);
                         temp.push_back(value_j);
                         temp.push_back(value_k);

                         std::sort(temp.begin(),temp.end());

                         //result.push_back(temp);
                            if(is_exist(result,temp)){
                               continue;
                            }
                            else{
                               result.push_back(temp);
                            }
                     }

                }
                else{
                    continue;
                }
           }
       }
         
        return result;
    }
};

344.反转字符串

自己写的,很常规的方法

class Solution {
public:
    void reverseString(vector<char>& s) {

       int count = s.size();
       int half = count / 2;

       //偶数 half-1;
       //奇数 half

       for(int i=0; i<half; i++){

           char temp = s[i];
           s[i] = s[count-i-1];
           s[count-i-1] = temp;
       }

       return;

    }
};

541.反转字符串2

自己写的,占用内存很高

class Solution {
public:
    string reverseStr(string s, int k) {

         string result;
         int length = s.length();
         if(length == 0) return s;

         int last = length % (2*k);
         int count = length / (2*k);

         for(int i=0; i<count; i++){
             std::string temp = s.substr(i*2*k,2*k);
             std::reverse(temp.begin(),temp.begin()+k);
             result += temp; 
         }

         if(last < k){
             string temp = s.substr(count*2*k,last);
             std::reverse(temp.begin(),temp.end());
             result += temp;
         }

         if(last >= k){
             string temp = s.substr(count*2*k,k);
             std::reverse(temp.begin(),temp.end());
             result += temp;

             temp = s.substr(count*2*k + k, last-k);
             result += temp;
         }

         return result;
    }
};

151.反转字符串中的单词

自己写的,时间很快,内存占用多些

class Solution {
public:
    string reverseWords(string s) {

        std::vector<string> data;
        bool is_start = false;

        int left = 0;
        int length = 0;

        for(int i=0; i<s.length(); i++){

            if(is_start == false){
               if(s[i] == ' '){
                   continue;
               }
               else{
                   is_start = true;
                   left = i;
                   length = 1;
               }
            }
            else{
               if(s[i] != ' '){
                   length++;
               }
               else{
                   std::string temp = s.substr(left,length);
                   data.push_back(temp);
                   is_start = false;
                   length = 0;
               }

            }

            if(i == s.length() - 1){
                if(is_start == true){
                   std::string temp = s.substr(left,length);
                   data.push_back(temp);
                   break;
                }
            }
        }

        std::string result;

        if(data.size() != 0){
           for(int i=data.size()-1; i>=0; i--){
               result += data.at(i);

               if(i!=0){
                  result += ' ';
               }

           }
        }

        return result;
    }
};
  1. 找出字符串中第一个匹配项的下标

自己写的,时间很快,内存有占用

class Solution {
public:
    int strStr(string haystack, string needle) {

          int start = 0;

          char n_start = needle[0];
          int n_size = needle.length();
          
          for(int i=0; i<haystack.length();i++){
              
              char h_start = haystack[i];

               if(h_start == n_start){
                      start = i;

                       int j=1;
                       while(j < n_size){
                           i++;
                           char h_next = haystack[i];
                           char n_next = needle[j];

                           if(h_next != n_next){
                                    i = start;
                               break;
                           }
                           else{
                               j++;
                           }

                       }

                       if(j == n_size){
                           return start;
                       }

                   }
                    
              }

              return -1;
          }

};
  1. 重复的子字符串

效率和内存占用都很一般,但是思路应该是对的

class Solution {
public:
    bool repeatedSubstringPattern(string s) {

           std::vector<std::string> sub_s;
           //int sub_size = 1;
           int s_size = s.length();
           bool repeated_exist = false;
           
           int m = 1;
           while(m*2 <= s_size){
               string first_s = s.substr(0,m);
               string second_s = s.substr(m,m);

               if(first_s == second_s){
                   repeated_exist = true;
                   sub_s.push_back(first_s);
                   //sub_size = m;
               }

               m++;
           }

           if(repeated_exist == false){
               return false;
           }

           repeated_exist = false;
           for(auto it : sub_s){

               int sub_size = it.length();
               if(s_size%sub_size != 0){
                   continue;
               }

                int n = s_size/sub_size;
               repeated_exist = true;
               for(int i=1; i<n; i++){
               
               string temp = s.substr(i*sub_size,sub_size);

               if(temp != it){
                   repeated_exist = false;
                   continue;
                }
              }

              if(repeated_exist == true){
                  return true;
              }
           }
            
           return repeated_exist;

    }
};

232.用栈实现队列

自己写的,效率和内存一般

class MyQueue {
public:
    MyQueue() {

    }
    
    void push(int x) {
         a.push(x);
    }
    
    int pop() {

          if(b.size() > 0){

              int value = b.top();
              b.pop();
              return value;
          }
          else{
               while(!a.empty()){
                  int value = a.top();
                  b.push(value);
                  a.pop();
                 }

                int value = b.top();
                b.pop();

                return value;
          }

    }
    
    int peek() {

        if(b.size() != 0){
            return b.top();
        }
        else{
                while(!a.empty()){
                    int value = a.top();
                    b.push(value);
                    a.pop();
                 }

                int value = b.top();

                return value;
        }

    }
    
    bool empty() {

          if(a.empty() && b.empty()){
              return true;
          }
          else{
              return false;
          }
    }

    std::stack<int> a;
    std::stack<int> b;
};

/**
 * Your MyQueue object will be instantiated and called as such:
 * MyQueue* obj = new MyQueue();
 * obj->push(x);
 * int param_2 = obj->pop();
 * int param_3 = obj->peek();
 * bool param_4 = obj->empty();
 */

225.用队列实现栈

自己写的,内存占用比较高

class MyStack {
public:
    MyStack() {

    }
    
    void push(int x) {
         a.push(x);
    }
    
    int pop() {

        int value;

         while(a.size() > 1){
             value = a.front();
             b.push(value);
             a.pop();
         }

        if(!a.empty()){
           value = a.front();
           a.pop();

           while(!b.empty()){
               int data = b.front();
               a.push(data);
               b.pop();
           }
        }

      return value;
    }

    
    int top() {

         int value;

         while(a.size() > 1){
             value = a.front();
             b.push(value);
             a.pop();
         }

         if(!a.empty()){
            value = a.front();
         }

       return value;
    }
    
    bool empty() {

        if(a.empty() && b.empty()){
            return true;
        }
        else{
            return false;
        }

    }

    std::queue<int> a;
    std::queue<int> b;
};

/**
 * Your MyStack object will be instantiated and called as such:
 * MyStack* obj = new MyStack();
 * obj->push(x);
 * int param_2 = obj->pop();
 * int param_3 = obj->top();
 * bool param_4 = obj->empty();
 */

20.有效的括号

class Solution {
public:
    bool isValid(string s) {

         std::stack<char> data;

         if(s.length() < 2) return false;

         for(int i=0; i<s.length(); i++){

            if(s[i] == ')' 
                || s[i] == ']'
                || s[i] == '}'){

                    if(data.size() == 0){
                        return false;
                    }
                }

             
             if(s[i] == '(' 
                || s[i] == '['
                || s[i] == '{'){
                    data.push(s[i]);
                }

             if(s[i] == ')'){
                 char match = data.top();

                 if(match == '('){
                     data.pop();
                 }else{
                     data.push(s[i]);
                 }
             }

             if(s[i] == ']'){
                 char match = data.top();

                 if(match == '['){
                     data.pop();
                 }
                 else{
                     data.push(s[i]);
                 }
             }

            if(s[i] == '}'){
                 char match = data.top();

                 if(match == '{'){
                     data.pop();
                 }else{
                     data.push(s[i]);
                 }
             }
           
         }

       if(data.size() == 0){
           return true;
       }
       else{
           return false;
       }
    }
};

150.逆波兰表达式求值

class Solution {
public:
    int evalRPN(vector<string>& tokens) {

        std::stack<string> data;

        int result;

        for(auto it : tokens){
             
            if(it == "+"){

                string first = data.top();
                int s_value = std::stoi(first);
                data.pop();

                string second = data.top();
                data.pop();
                int f_value = std::stoi(second);

                result = f_value + s_value;
                string s_type = std::to_string(result);
                data.push(s_type);

            }
            else if(it == "-"){

                                string first = data.top();
                int s_value = std::stoi(first);
                data.pop();

                string second = data.top();
                data.pop();
                int f_value = std::stoi(second);

                result = f_value - s_value;
                string s_type = std::to_string(result);
                data.push(s_type);

            }
            else if(it == "*"){

                string first = data.top();
                int s_value = std::stoi(first);
                data.pop();

                string second = data.top();
                data.pop();
                int f_value = std::stoi(second);

                result = f_value * s_value;
                string s_type = std::to_string(result);
                data.push(s_type);
 
            }
            else if(it == "/"){

                string first = data.top();
                int s_value = std::stoi(first);
                data.pop();

                string second = data.top();
                data.pop();
                int f_value = std::stoi(second);

                result = f_value / s_value;
                
                string s_type = std::to_string(result);
                data.push(s_type);
            }
            else{
                data.push(it);
                result = std::stoi(it);
            }
        }

        return result;

    }
};

239.滑动窗口最大值

cpu占用比较高,自己写的

class Solution {
public:
    vector<int> maxSlidingWindow(vector<int>& nums, int k) {

        vector<int> result;
        int max_value;
        std::priority_queue<int> range_data;
        bool need_refine = false;

        for(int i=0; i<k; i++){
            range_data.push(nums[i]);
        }

        if(range_data.size() != 0){
             max_value = range_data.top();
             result.push_back(max_value);
        }

        for(int i=0; i < nums.size() - k; i++){
             
             int remove_it = nums[i];
             int add_it = nums[i+k];

             if(remove_it == max_value){
                 range_data.pop();

                    if(need_refine == true){
                        while(!range_data.empty()){
                          range_data.pop();
                        }

                        for(int j=0; j<k-1; j++){
                         range_data.push(nums[i+1+j]);
                        }

                        need_refine = false;
                    }

             }
             else{
                  need_refine = true;
             }
             
             range_data.push(add_it);

             max_value = range_data.top();
             result.push_back(max_value);

        }

        return result;
 
    }
};

347.前K个高频元素

自己写的,一般般

class Solution {
public:
    vector<int> topKFrequent(vector<int>& nums, int k) {

           std::map<int,int> data;
           vector<int> result;

           for(int i=0; i<nums.size(); i++){
               if(data.find(nums[i]) == data.end()){
                   data.emplace(nums[i],1);
               }
               else{
                   int count = data.at(nums[i]);
                   data.at(nums[i]) = count + 1;
               }
           }


           //
           std::priority_queue<int> k_sort;

           for(auto it : data){
               k_sort.push(it.second);
           }

           //
           for(int i=0; i<k; i++){
               k_sort.pop();
           }

            int top_value = 0;
           if(k_sort.size() > 0){
               top_value = k_sort.top();
           }

           //
           for(auto it : data){
               if(it.second > top_value){
                  result.push_back(it.first);
               }
           }

           //
           return result;


    }
};

42.接雨水

自己写的,cpu占用比较高

class Solution {
public:
    int trap(vector<int>& height) {
           
           int left = 0;
           //int middle = 0;
           int right = 0;
           bool find_left = false;
           bool find_middle = false;
           bool find_right = false;

           int left_h = 0;
           //int middle_h = 0;
           int right_h = 0;

           int count = 0;
           //int test = 0;

           //vector pools;

           for(int i=0; i<height.size(); i++){

                int value = height.at(i);

                int next_i = i + 1;
                int next_value = -1;
                if(next_i < height.size()){
                   next_value  = height.at(next_i);
                }
               
               if(find_left == false){
                   if(next_value < value){
                      left = i;
                      left_h = value;
                      find_left = true;
                   }
               }
     
               if(find_left == true 
                  && find_middle == false){
                       if(next_value > value){
                           //middle = i;
                           //middle_h = value;
                           find_middle = true;
                       }

                }

                if(find_middle == true
                   && find_right == false){

                      if(value >= left_h){
                          right = i;
                          right_h = value;
                          find_right = true;
                      }
                      else{
                          if(value > right_h){
                              right_h = value;
                              right = i;
                          }
                      }

                      if(i== height.size()-1){
                          find_right = true;
                      }

                   }

               if(find_right == true){

                    // test++;
                    // if(test == 1){
                    //     return right;
                    // }

                   int pool_h = std::min(left_h, right_h);
                   //count = 0;
                   for(int j=left+1; j<right;j++){

                       int water = (pool_h - height.at(j));
                       
                       if(water > 0){
                           count += water;
                       }
                       
                   }

                   //pools.push_back(count);

                   i = right-1;
                   find_left = false;
                   find_middle = false;
                   find_right = false;
                   left_h = 0;
                   //middle_h = 0;
                   right_h = 0;

               }

           }

           return count;
           
    }
};

144.二叉树的前序遍历

使用递归的方式,使用栈的方式运行速度更快

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:

   void reloop(vector<int>& result, TreeNode* node){
           result.push_back(node->val);

           if(node->left != nullptr){
               reloop(result, node->left);
           }
           else{
               //result.push_back(0);
           }

           if(node->right != nullptr){
               reloop(result,node->right);
           }
           else{
               //result.push_back(0);
           }
   }


    vector<int> preorderTraversal(TreeNode* root) {
               
          vector<int> result;

          if(root != nullptr){
             reloop(result,root);
          }
          else{
              //result.push_back(nullptr);
          }

          return result;
    }
};
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:

    vector<int> preorderTraversal(TreeNode* root) {
               
          //使用栈的方式
          std::stack<TreeNode*> data;
          data.push(root);
          vector<int> result;

          while(!data.empty()){

               TreeNode* item = data.top();
               data.pop();

               if(item != nullptr){
                   result.push_back(item->val);

                   data.push(item->right);
                   data.push(item->left);
               }

          }

          return result;
    }
};
  1. 二叉树的后序遍历

递归写法

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:

   void reloop(vector<int>& result, TreeNode* node){

        if(node->left != nullptr){
            reloop(result,node->left);
        }

        if(node->right != nullptr){
            reloop(result,node->right);
        }

        result.push_back(node->val);

   }


    vector<int> postorderTraversal(TreeNode* root) {

          vector<int> result;

          if(root == nullptr){
              return result;
          }
          else{
              reloop(result,root);
          }


          return result;


    }
};

102.二叉树的层序遍历

使用队列实现

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    vector<vector<int>> levelOrder(TreeNode* root) {
                
           vector<vector<int>> result;

           if(root == nullptr){
               return result;
           }

           std::queue<TreeNode*> data;
           data.push(root);

           while(!data.empty()){

                vector<int> temp;
                int count = data.size();
                for(int i=0; i<count; i++){
                    auto it = data.front();
                    data.pop();
                    temp.push_back(it->val);

                    if(it->left != nullptr){
                        data.push(it->left);
                    }

                    if(it->right != nullptr){
                        data.push(it->right);
                    }
                }

                result.push_back(temp);

           }

           return result;     
    }
};

226.翻转二叉树

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:

    void reverse(TreeNode* node){
        TreeNode* temp = node->right;
        node->right = node->left;
        node->left = temp;
    }

    void reloop(TreeNode* node){

        if(node == nullptr) return;

        //if(node->left != nullptr && node->right != nullptr){
            reverse(node);
            reloop(node->left);
            reloop(node->right);
        //}
    }

    TreeNode* invertTree(TreeNode* root) {

        if(root == nullptr){
            return root;
        }

        reloop(root);

        return root;
    }
};

101.对称二叉树

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:

    bool reloop(TreeNode* left, TreeNode* right){

         if(left == nullptr && right == nullptr){
             return true;
         }
         else if(left == nullptr && right != nullptr){
             return false;
         }
         else if(left != nullptr && right == nullptr){
             return false;
         }
         else if(left->val == right->val){
              bool state_1 = reloop(left->left, right->right);
              bool state_2 = reloop(left->right, right->left);

              return state_1&&state_2;
         }
         else{
             return false;
         }

    }

    bool isSymmetric(TreeNode* root) {

        if(root == nullptr){
            return true;
        }
        else{
            return reloop(root->left, root->right);
        }

    }
};

104.二叉树的最大深度

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:

    int reloop(TreeNode* root, int depth){
        
        int depth_l = depth;
        int depth_r = depth;
            
        if(root->left != nullptr){
              depth_l = depth + 1;
              depth_l = reloop(root->left,depth_l);
        }

        if(root->right != nullptr){
             depth_r = depth + 1;
             depth_r = reloop(root->right,depth_r);
        }

        return std::max(depth_l,depth_r);
    }


    int maxDepth(TreeNode* root) {
            
           int depth = 1;

           if(root == nullptr){
               return 0;
           }
           else{
               return reloop(root,depth);
           }

    }
};

111.二叉树的最小深度

自己写的,效率不高

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:

    int reloop(TreeNode* node, int& depth){

        int depth_l = depth;
        int depth_r = depth;
        int result;

        if(node->left != nullptr && node->right == nullptr){
            depth_l = depth + 1;
            result = reloop(node->left, depth_l);
        }

        if(node->right != nullptr && node->left == nullptr){
            depth_r = depth_r + 1;
            result = reloop(node->right, depth_r);
        }

        if(node->left == nullptr && node->right == nullptr){
            result = depth;
        }

        if(node->left != nullptr && node->right != nullptr){
              depth_l = depth + 1;
              depth_r = depth + 1;

              depth_l = reloop(node->left, depth_l);
              depth_r = reloop(node->right, depth_r);

              result = std::min(depth_l,depth_r);
        }
        
        return result;
    }


    int minDepth(TreeNode* root) {

           int depth = 1;
           int result;

           if(root == nullptr){
               return 0;
           }
           else{

               if(root->left == nullptr && root->right != nullptr){
                   depth = 2;
                   result = reloop(root->right,depth);
               }

               if(root->left != nullptr && root->right == nullptr){
                   depth = 2;
                   result = reloop(root->left, depth);
               }

               if(root->left == nullptr && root->right == nullptr){
                   return 1;
               }

               if(root->left != nullptr && root->right != nullptr){
                   result = reloop(root,depth);
               }
           } 

           return result;

    }
};
  1. 平衡二叉树

自己写的,内存占用比较多

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:

    int reloop(TreeNode* node, int& depth, bool& isBalanced){

         bool balanced_r = true;
         bool balanced_l = true;
         int depth_l = depth;
         int depth_r = depth;

         if(node->left == nullptr && node->right == nullptr){
             //return depth;
         }      

         if(node->left == nullptr && node->right != nullptr){
                 depth_r = depth_r + 1;
                 depth_r = reloop(node->right, depth_r, balanced_r);

                 if(balanced_r == false 
                    || std::abs(depth_l - depth_r) > 1){
                        isBalanced = false;
                    }

                    depth =  depth_r;

         }

         if(node->left != nullptr && node->right == nullptr){
                 depth_l = depth_l + 1;
                 depth_l = reloop(node->left, depth_l, balanced_l);

                if(balanced_l == false 
                    || std::abs(depth_l - depth_r) > 1){
                        isBalanced = false;
                    }

                    depth = depth_l;
         }

         if(node->left != nullptr && node->right != nullptr){

                 
                 depth_r = depth_r + 1;
                 depth_r = reloop(node->right, depth_r, balanced_r);

                 depth_l = depth_l + 1;
                 depth_l = reloop(node->left, depth_l, balanced_l);

                 if(balanced_r == false 
                    || balanced_l == false
                    || std::abs(depth_l - depth_r) > 1){
                        isBalanced = false;
                    }

                depth =  std::max(depth_l, depth_r);
         }

         return depth;
    }


    bool isBalanced(TreeNode* root) {

            if(root == nullptr){
                return true;
            }
            else{
 
                bool state = true;
                int depth = 1;
                reloop(root, depth, state);

                return state;
            }

    }
};

257.二叉树的所有路径

内存占用大,自己写的一次过

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:

   void reloop(TreeNode* node, vector<int> result, vector<vector<int>>& data){

           vector<int> l_result = result;
           vector<int> r_result = result;

           if(node->left != nullptr){
              
              l_result.push_back(node->left->val);
              reloop(node->left, l_result, data);

           }

           if(node->right != nullptr){
               
               r_result.push_back(node->right->val);
               reloop(node->right, r_result, data);
           }

           if(node->left == nullptr && node->right == nullptr){
                 data.push_back(result);
           }

   }


    vector<string> binaryTreePaths(TreeNode* root) {

        vector<vector<int>> final_data;
        vector<int> middle_data;
        vector<string> final_result;

        if(root == nullptr){
            return final_result;
        }
        else{

            middle_data.push_back(root->val);
            reloop(root, middle_data, final_data);


            for(int i=0; i<final_data.size(); i++){

                vector<int> item = final_data.at(i);
                string data;
                for(int j=0; j<item.size(); j++){
                    data += std::to_string(item.at(j));
                    if(j != item.size() - 1){
                        data += "->";
                    }
                }

                final_result.push_back(data);

            }

        }

        return final_result;

    }
};

112.路径总和

自己写的,常规思路

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:

    bool reloop(TreeNode* node, int sum, int target){

          bool result_l = false;
          bool result_r = false;

          if(node->left != nullptr){
              result_l = reloop(node->left,sum+node->left->val,target);
          }

          if(node->right != nullptr){
              result_r = reloop(node->right,sum+node->right->val,target);
          }

          if(node->left == nullptr && node->right == nullptr){
              if(target == sum){
                  return true;
              }
              else{
                  return false;
              }
          }

          if(result_l == true || result_r == true){
              return true;
          }
          else{
              return false;
          }

    }


    bool hasPathSum(TreeNode* root, int targetSum) {
           
           if(root == nullptr){
               return false;
           }
           else{
             return reloop(root,root->val,targetSum);
           }
    }
};
  1. 从中序与后序遍历序列构造二叉树

规律找到,一次过,不过效率不是最高

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:

    void find_subtree_range(vector<int>& inorder_mark, 
                            int start_index,
                            int& l_range, 
                            int& r_range){

          for(int i=start_index+1; i<inorder_mark.size(); i++){
                    if(inorder_mark.at(i) == 1){
                        break;
                    }
                    else{
                        r_range++;
                    }
                }

            
           for(int i=start_index-1; i>=0; i--){
                    if(inorder_mark.at(i) == 1){
                        break;
                    }
                    else{
                        l_range++;
                    }
                }    
                
    }

    TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {


          int root_val = postorder.at(postorder.size() - 1);
          TreeNode* root = new TreeNode(root_val);

          vector<int> inorder_mark;
          std::map<int,int> inorder_mapper;
          std::map<int,int> postorder_mapper;

          for(int i=0; i<inorder.size(); i++){
              inorder_mark.push_back(0);
              inorder_mapper.emplace(inorder.at(i), i);
              postorder_mapper.emplace(postorder.at(i), i);
          }

          std::queue<TreeNode*> data_queue;
          data_queue.push(root);
          inorder_mark.at(inorder_mapper.at(root_val)) = 1;

          while(!data_queue.empty()){

                auto item = data_queue.front();
                data_queue.pop();
                
                int inorder_index = inorder_mapper.at(item->val);
                int postorder_index = postorder_mapper.at(item->val);

                int r_range = 0;
                int l_range = 0;

                find_subtree_range(inorder_mark,
                                   inorder_index,
                                   l_range,
                                   r_range);

                if(l_range>0){

                    int l_index = postorder_index - r_range - 1;
                    int l_val = postorder.at(l_index); 
                    TreeNode* l_node = new TreeNode(l_val);

                    item->left = l_node;
                    data_queue.push(l_node);

                    inorder_mark.at(inorder_mapper.at(l_val)) = 1;
                }


                 if(r_range > 0){
                     int r_index = postorder_index - 1;
                     int r_val = postorder.at(r_index);
                     TreeNode* r_node = new TreeNode(r_val);

                     item->right = r_node;
                     data_queue.push(r_node);
                     inorder_mark.at(inorder_mapper.at(r_val)) = 1;
                 }

          }

          return root;

    }
};
  1. 从前序与中序遍历构造二叉树

自己写的,效率不高

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:

    void find_submap_range(vector<int>& inorder_mark, 
                           int start_index,
                           int& left_range,
                           int& right_range){

            for(int i=start_index+1; i< inorder_mark.size(); i++){
                if(inorder_mark.at(i) == 1){
                    break;
                }
                else{
                    right_range++;
                }
            }

            for(int i=start_index-1; i>=0; i--){
                 if(inorder_mark.at(i) == 1){
                     break;
                 }
                 else{
                     left_range++;
                 }
            }

 }



    TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {

            int root_val = preorder.at(0);
            TreeNode* root = new TreeNode(root_val);

            std::map<int,int> preorder_mapper;
            std::map<int,int> inorder_mapper;
            vector<int> inorder_mark;

            for(int i=0; i<inorder.size(); i++){
                inorder_mark.push_back(0);
                preorder_mapper.emplace(preorder.at(i), i);
                inorder_mapper.emplace(inorder.at(i), i);
            }


            std::queue<TreeNode*> data_queue;
            data_queue.push(root);
            inorder_mark.at(inorder_mapper.at(root_val)) = 1;


            while(!data_queue.empty()){

                 auto item = data_queue.front();
                 data_queue.pop();


                 int left_range = 0;
                 int right_range = 0;
                 int start_index = inorder_mapper.at(item->val);
                 int preorder_index = preorder_mapper.at(item->val);

                 find_submap_range(inorder_mark,
                                   start_index,
                                   left_range,
                                   right_range);

                 if(left_range > 0){

                      int l_index = preorder_index + 1;
                      int l_val = preorder.at(l_index);

                      TreeNode* l_node = new TreeNode(l_val);
                      item->left = l_node;
                      data_queue.push(l_node);
                      inorder_mark.at(inorder_mapper.at(l_val)) = 1;
                 }

                 if(right_range > 0){
                      int r_index = preorder_index + left_range + 1;
                      int r_val = preorder.at(r_index);

                      TreeNode* r_node = new TreeNode(r_val);
                      item->right = r_node;
                      data_queue.push(r_node);
                      inorder_mark.at(inorder_mapper.at(r_val)) = 1;

                 }

            }

        return root;
    }
};

617 合并二叉树

自己写的,效率一般

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:

    void reloop(TreeNode* combined, TreeNode* root1, TreeNode* root2){

        if(root1 == nullptr && root2 == nullptr){
            return;
        }

        if(root1 != nullptr && root2 != nullptr){
            combined->val = root1->val+root2->val;

            if(root1->left != nullptr || root2->left != nullptr){
            TreeNode* l_node = new TreeNode();
            combined->left = l_node;
            reloop(combined->left, root1->left, root2->left); 
            }

            if(root1->right != nullptr || root2->right != nullptr){
            TreeNode* r_node = new TreeNode();
            combined->right = r_node;
            reloop(combined->right, root1->right, root2->right); 
            }

            return;
        }

        if(root1 == nullptr && root2 != nullptr){
            combined->val = root2->val;
            
            if(root2->left != nullptr){
            TreeNode* l_node = new TreeNode();
            combined->left = l_node;
            reloop(combined->left, nullptr, root2->left); 
            }
 
            if(root2->right != nullptr){
            TreeNode* r_node = new TreeNode();
            combined->right = r_node;
            reloop(combined->right, nullptr, root2->right); 
            }
            return;
        }

        if(root1 != nullptr && root2 == nullptr){
            combined->val = root1->val;

            if(root1->left != nullptr){
               TreeNode* l_node = new TreeNode();
               combined->left = l_node;
               reloop(combined->left, root1->left, nullptr); 
            }

            if(root1->right != nullptr){
               TreeNode* r_node = new TreeNode();
               combined->right = r_node;
               reloop(combined->right, root1->right, nullptr); 
            }

            return;
        }

    }


    TreeNode* mergeTrees(TreeNode* root1, TreeNode* root2) {

        TreeNode* root = nullptr;

        if(root1 == nullptr && root2 == nullptr){
            return nullptr;
        }

        root = new TreeNode();

        reloop(root, root1, root2);
                                 
        return root;
    }
};

700 二叉搜索树的搜索

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:

    TreeNode* searchBST(TreeNode* root, int val) {

         
         std::queue<TreeNode*> data_queue;
         data_queue.push(root);

         while(!data_queue.empty()){

             auto item = data_queue.front();
             int item_val = item->val;
             data_queue.pop();

             if(item_val == val){
                 return item;
             }
             else{

               if(item->left != nullptr){
                   data_queue.push(item->left);
               }

               if(item->right != nullptr){
                   data_queue.push(item->right);
               }

             }
         }

         return nullptr;

    }
};

98.验证二叉搜索树

中序遍历,然后排序检查

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:


    void reloop(TreeNode* node, vector<int>& result){
         
         if(node != nullptr){

             if(node->left != nullptr){
                 reloop(node->left, result);
             }

             result.push_back(node->val);

             if(node->right != nullptr){
                 reloop(node->right,result);
             }
         }

         return;
    }


    bool isValidBST(TreeNode* root) {

             vector<int> result;

             reloop(root, result);

             for(int i=0; i<result.size(); i++){

                  int j = i + 1;
                  if(j< result.size()){
                       
                       if(result[j] <= result[i]){
                           return false;
                       }

                  }
                  else{
                      break;
                  }   
             }


             return true;

    }
};
  1. 二叉搜索树的最小绝对差
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:

   
     void reloop(TreeNode* node, vector<int>& result){
         if(node == nullptr){
             return;
         }

         if(node->left != nullptr){
             reloop(node->left, result);
         }

         result.push_back(node->val);

         if(node->right != nullptr){
             reloop(node->right, result);
         }
     }

    int getMinimumDifference(TreeNode* root) {
           
           vector<int> result;

           reloop(root, result);

           if(result.size()<2){
               return -1;
           }

           int min_range = result.at(1) - result.at(0);

           for(int i=0; i<result.size(); i++){

               int j= i+1;
               if(j < result.size()){

                  int value = result.at(j) - result.at(i);

                  if(value < min_range){
                      min_range = value;
                  }
               }
               else{
                   break;
               }
           }

           return min_range;
    }
};
  1. 二叉搜索树中的众数
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:

    void reloop(TreeNode* node, vector<int>& result){

        if(node == nullptr){
            return;
        }

        if(node->left != nullptr){
            reloop(node->left, result);
        }

        result.push_back(node->val);

        if(node->right != nullptr){
            reloop(node->right, result);
        }

    }


    vector<int> findMode(TreeNode* root) {

             vector<int> result;

             reloop(root, result);

             std::map<int,int> result_mapper;

             for(auto it : result){

                   if(result_mapper.find(it) == result_mapper.end()){
                       result_mapper.emplace(it,1);
                   }
                   else{
                       int count = result_mapper.at(it);
                       result_mapper.at(it) = count + 1;
                   }
             }

             int max_val = 0;
             int max_count = 0;


             for(auto it : result_mapper){
                 int count = it.second;

                 if(count > max_count){
                     max_count = count;
                     max_val = it.first;
                 }
             }

             vector<int> max_result;

             for(auto it : result_mapper){
                 if(it.second == max_count){
                     max_result.push_back(it.first);
                 }
             }


             return max_result;

    }
};
  1. 二叉树的最近公共祖先

效率低一些

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

    void find_father(TreeNode* node, vector<int>&result, 
                     bool& find, int target){

           if(find == true){
               result.push_back(node->val);
           }              

           if(node == NULL){
               return;
           }
            else{

                if(node->val == target){
                    find = true;
                    result.push_back(target);
                    return;
                }

                if(node->left != NULL){
                    bool l_find = false;
                    find_father(node->left, result,l_find,target);
                    if(l_find == true){
                        result.push_back(node->val);
                        find = true;
                    }
                }

                if(node->right != NULL){
                    bool r_find = false;
                    find_father(node->right, result, r_find, target);
                    if(r_find == true){
                        result.push_back(node->val);
                        find = true;
                    }

                }
            }
    }


    TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
        
        vector<int> p_father;
        bool p_find = false;
        vector<int> q_father;
        bool q_find = false;

        find_father(root,p_father,p_find,p->val);
        find_father(root,q_father,q_find,q->val);


        if(p_father.size() == 2){
            TreeNode* node = new TreeNode(111);
            //return node;
        }

        if(q_father.size() == 4){
            TreeNode* node = new TreeNode(q_father.at(2));
            //return node;
        }

        vector<int> down_father;
        vector<int> up_father;
        if(q_father.size() > p_father.size()){
             down_father = q_father;
             up_father = p_father;
        }
        else{
            down_father = p_father;
            up_father = q_father;
        }

        int result = 0;

        for(int i=0; i< down_father.size(); i++){

             int value_i = down_father.at(i);

             bool find = false;
             for(int j=0; j<up_father.size(); j++){

                  int value_j = up_father.at(j);

                  if(value_i == value_j){

                      result = value_j;
                      find = true;
                  }
             }

             if(find == true){
                 break;
             }
        }

        TreeNode* node = new TreeNode(result);
        return node;
    }
};

701 二叉搜索树中的插入操作

效率还可以,内存占用较多

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:

    TreeNode* find_tree(TreeNode* node, int val){

        std::queue<TreeNode*> data_queue;
        data_queue.push(node);

        TreeNode* result = nullptr;

        while(!data_queue.empty()){

            auto item = data_queue.front();
            data_queue.pop();

            if(item->val == val){
                result = item;
                break;
            }

            if(item->left != nullptr){
                data_queue.push(item->left);
            }

            if(item->right != nullptr){
                data_queue.push(item->right);
            }
        }

        return result;
    }


    void reloop(TreeNode* node, vector<int>& result, int target){

         if(node == nullptr){
             return;
         }

         if(node->left != nullptr){
             reloop(node->left, result, target);
         }

         result.push_back(node->val);
         
         if(node->val > target){
             return;
          }

        if(node->right != nullptr){
            reloop(node->right, result, target);
        }

    }


    TreeNode* insertIntoBST(TreeNode* root, int val) {

            vector<int> result;
            //fisrt step
            reloop(root, result, val);

            if(result.size() == 0){
                TreeNode* node = new TreeNode(val);
                return node;
            }

            int left_val = 0;
            int right_val = 0;
            bool left_find = false;
            bool right_find = false;
           
           //second step
           for(int i=0; i<result.size(); i++){

                int value = result.at(i);

                if(val > value){
                    left_find = true;
                    left_val = value;
                }

                if(val < value){
                    right_find = true;
                    right_val = value;
                    break;
                }

            }

            //third step
            TreeNode* l_node = nullptr;
            TreeNode* r_node = nullptr;

            if(left_find == true){
                l_node = new TreeNode();
                l_node = find_tree(root, left_val);
            }

            if(right_find == true){
                r_node = new TreeNode();
                r_node = find_tree(root, right_val);
            }

            //final step
            TreeNode* new_node = new TreeNode(val);

            if(l_node != nullptr && r_node != nullptr){

                if(l_node->right == nullptr){
                    l_node->right = new_node;
                }
                else{

                    TreeNode* temp = l_node->right;

                    l_node->right = new_node;
                    new_node->right = temp;
                }

            }
    
            if(l_node != nullptr && r_node == nullptr){
                l_node->right = new_node;
            }

            if(l_node == nullptr && r_node != nullptr){
                r_node->left = new_node;
            }

            return root;
    }
};
  1. 删除二叉搜索树中的节点

坑此坑次,过了,效率和内存一般

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:

    TreeNode* find_subtree(TreeNode* root,    
                      int val){

        std::queue<TreeNode*> data_queue;
        data_queue.push(root);

        while(!data_queue.empty()){

            TreeNode* item = data_queue.front();
            data_queue.pop();

            if(item->val == val){
                return item;
            }

            if(item->left != nullptr){
                data_queue.push(item->left);
            }

            if(item->right != nullptr){
                data_queue.push(item->right);
            }

        }

        return nullptr;
    }

    TreeNode* find_fatherTree(TreeNode* root,
                              std::string& way,
                              int val ){

        if(root == nullptr){
              return nullptr;
        }
        else{
           if(root->val == val){
              return root;
            }
        }

        std::queue<TreeNode*> data_queue;
        data_queue.push(root);

        while(!data_queue.empty()){

            auto item = data_queue.front();
            data_queue.pop();

            if(item->left != nullptr){

                if(item->left->val == val){
                    way = "left";
                    return item;
                }
                else{
                   data_queue.push(item->left);
                }
            }

            if(item->right != nullptr){

                if(item->right->val == val){
                    way = "right";
                    return item;
                }
                else{
                    data_queue.push(item->right);
                }
            }

        }

        return nullptr;
    }

    TreeNode* find_left(TreeNode* root){

        TreeNode* node = root;

         while(node->left != nullptr){
             node = node->left;
         }

         return node;
    }

    TreeNode* deleteNode(TreeNode* root, int key) {

            if(root == nullptr){
                return nullptr;
            }

            std::string f_way = "";

            TreeNode* b_node = find_subtree(root, key);
            TreeNode* f_node = find_fatherTree(root, f_way, key);

            if(b_node == nullptr){
                return root;
            }

            if(f_node == nullptr){
                return nullptr;
            }

            //if b_find != nullptr
            if(f_node->val == key){
                 
                 if(b_node->right == nullptr){
                     return b_node->left;
                 }
                 else{
                    TreeNode* left_node = find_left(b_node->right);
                    left_node->left = b_node->left;
                    return b_node->right;
                 }

            }
            else{
 
                if(b_node->right == nullptr){

                    if(b_node->left != nullptr){
                        
                        if(f_way == "left"){
                           f_node->left = b_node->left;
                        }
                        
                        if(f_way == "right"){
                            f_node->right = b_node->left;
                        }
                    }
                    else{
                        if(f_way == "left"){
                            f_node->left = nullptr;
                        }
                        
                        if(f_way == "right"){
                            f_node->right = nullptr;
                        }
                    }

                    //return root;
                }
                else{
                    
                    if(f_way == "left"){
                        f_node->left = b_node->right;
                        TreeNode* left_node = find_left(b_node->right);
                        left_node->left = b_node->left;
                    }
  
                    if(f_way == "right"){
                        f_node->right = b_node->right;
                        TreeNode* left_node = find_left(b_node->right);
                        left_node->left = b_node->left;
                    }

                    //return root;
                }

            }

        return root;

    }
};
  1. 修剪二叉搜索树

效率和内存占用不忍直视

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {


public:

    TreeNode* find_subtree(TreeNode* root,    
                      int val){

        std::queue<TreeNode*> data_queue;
        data_queue.push(root);

        while(!data_queue.empty()){

            TreeNode* item = data_queue.front();
            data_queue.pop();

            if(item->val == val){
                return item;
            }

            if(item->left != nullptr){
                data_queue.push(item->left);
            }

            if(item->right != nullptr){
                data_queue.push(item->right);
            }

        }

        return nullptr;
    }

    TreeNode* find_fatherTree(TreeNode* root,
                              std::string& way,
                              int val ){

        if(root == nullptr){
              return nullptr;
        }
        else{
           if(root->val == val){
              return root;
            }
        }

        std::queue<TreeNode*> data_queue;
        data_queue.push(root);

        while(!data_queue.empty()){

            auto item = data_queue.front();
            data_queue.pop();

            if(item->left != nullptr){

                if(item->left->val == val){
                    way = "left";
                    return item;
                }
                else{
                   data_queue.push(item->left);
                }
            }

            if(item->right != nullptr){

                if(item->right->val == val){
                    way = "right";
                    return item;
                }
                else{
                    data_queue.push(item->right);
                }
            }

        }

        return nullptr;
    }

    TreeNode* find_left(TreeNode* root){

        TreeNode* node = root;

         while(node->left != nullptr){
             node = node->left;
         }

         return node;
    }

    TreeNode* deleteNode(TreeNode* root, int key) {

            if(root == nullptr){
                return nullptr;
            }

            std::string f_way = "";

            TreeNode* b_node = find_subtree(root, key);
            TreeNode* f_node = find_fatherTree(root, f_way, key);

            if(b_node == nullptr){
                return root;
            }

            if(f_node == nullptr){
                return nullptr;
            }

            //if b_find != nullptr
            if(f_node->val == key){
                 
                 if(b_node->right == nullptr){
                     return b_node->left;
                 }
                 else{
                    TreeNode* left_node = find_left(b_node->right);
                    left_node->left = b_node->left;
                    TreeNode* new_node = b_node->right;
                    b_node->left = nullptr;
                    b_node->right = nullptr;
                    return new_node;
                 }

            }
            else{
 
                if(b_node->right == nullptr){

                    if(b_node->left != nullptr){
                        
                        if(f_way == "left"){
                           f_node->left = b_node->left;
                        }
                        
                        if(f_way == "right"){
                            f_node->right = b_node->left;
                        }
                    }
                    else{
                        if(f_way == "left"){
                            f_node->left = nullptr;
                        }
                        
                        if(f_way == "right"){
                            f_node->right = nullptr;
                        }
                    }

                    //return root;
                }
                else{
                    
                    if(f_way == "left"){
                        f_node->left = b_node->right;
                        TreeNode* left_node = find_left(b_node->right);
                        left_node->left = b_node->left;
                    }
  
                    if(f_way == "right"){
                        f_node->right = b_node->right;
                        TreeNode* left_node = find_left(b_node->right);
                        left_node->left = b_node->left;
                    }

                    //return root;
                }

            }

        return root;

    }

    void reloop(TreeNode* node, vector<int>& result){

        if(node == nullptr){
            return;
        }

        if(node->left != nullptr){
            reloop(node->left, result);
        }

        result.push_back(node->val);

        if(node->right != nullptr){
            reloop(node->right, result);
        }
    }
    
public:
    TreeNode* trimBST(TreeNode* root, int low, int high) {

             vector<int> result;
             reloop(root, result);

             TreeNode* final_root = root;

             for(auto it : result){

                if(it < low || it > high){
                     final_root = deleteNode(final_root,it);
                }

             }

             return final_root;
    }
};
  1. 将有序数组转换为二叉搜索树


```cpp
```/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:

    void find_subtree_range(vector<int>& inorder_mark, 
                            int start_index,
                            int& l_range, 
                            int& r_range){

          for(int i=start_index+1; i<inorder_mark.size(); i++){
                    if(inorder_mark.at(i) == 1){
                        break;
                    }
                    else{
                        r_range++;
                    }
                }

            
           for(int i=start_index-1; i>=0; i--){
                    if(inorder_mark.at(i) == 1){
                        break;
                    }
                    else{
                        l_range++;
                    }
                }    
                
    }

    TreeNode* sortedArrayToBST(vector<int>& nums) {


          int root_val = nums.at(nums.size() / 2);
          TreeNode* root = new TreeNode(root_val);

          vector<int> inorder_mark;
          std::map<int,int> inorder_mapper;

          for(int i=0; i<nums.size(); i++){
              inorder_mark.push_back(0);
              inorder_mapper.emplace(nums.at(i), i);
          }

          std::queue<TreeNode*> data_queue;
          data_queue.push(root);
          inorder_mark.at(inorder_mapper.at(root_val)) = 1;

          while(!data_queue.empty()){

                auto item = data_queue.front();
                data_queue.pop();
                
                int inorder_index = inorder_mapper.at(item->val);

                int r_range = 0;
                int l_range = 0;

                find_subtree_range(inorder_mark,
                                   inorder_index,
                                   l_range,
                                   r_range);

                if(l_range>0){

                    int l_index = inorder_index - (l_range+1)/2;
                    int l_val = nums.at(l_index); 
                    TreeNode* l_node = new TreeNode(l_val);

                    item->left = l_node;
                    data_queue.push(l_node);

                    inorder_mark.at(inorder_mapper.at(l_val)) = 1;
                }


                 if(r_range > 0){
                     int r_index = inorder_index + (r_range+1)/2;
                     int r_val = nums.at(r_index);
                     TreeNode* r_node = new TreeNode(r_val);

                     item->right = r_node;
                     data_queue.push(r_node);
                     inorder_mark.at(inorder_mapper.at(r_val)) = 1;
                 }

          }

          return root;
          

    }
};

你可能感兴趣的:(C++/Qt自动化框架,leetcode,算法,c++)