Leetcode binary tree finishing

Check carefully (whether the function name is the same, the variable name is the same, if there is a problem with the if statement, and the return value type)
1. Punctuation (semicolon)
2. Judgment statement (==)
3. Whether the function input parameters correspond to each other (definitely Check carefully!!)
4. Whether the loop is out of bounds
5. Whether the invariant in the loop has changed (for example, whether the size in the linked list title has changed)

**

Question 617: Merging Binary Trees

**
Insert picture description here
Use recursion to solve problems, pre-order traversal, pay attention to clearly write the function! ! !
Insert picture description here
**

700: Binary search tree

**
Insert picture description here
Use recursive method to find separately
Insert picture description here

98: Verify the binary search tree

Insert picture description here
Traverse the output array in middle order and judge whether the array is in order! ! !
Check carefully (whether the function name is the same, the variable name is the same, whether there is a problem with the if statement, and the return value type)
Insert picture description here

530: The minimum absolute difference of the binary search tree

Insert picture description here
Convert the binary search tree into an array, so as to find the minimum absolute difference through the array
Insert picture description here

/**
 * 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 travel(TreeNode* node,vector<int>& re){
    
    
        if(node==NULL) return;
        travel(node->left,re);
        re.push_back(node->val);
        travel(node->right,re);
    }
    int getMinimumDifference(TreeNode* root) {
    
    
        vector<int> re;
        travel(root,re);
        int x=INT_MAX;
        if(re.size()==0||re.size()==1) return 0;
        for(int i=0;i<re.size()-1;i++){
    
    
            x=min(x,abs(re[i]-re[i+1]));
        }
        return x;

    }
};

Question 501: Mode of Binary Search Tree

Insert picture description here

Solve using traversal

/**
 * 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 maxCount;
    int count;
    TreeNode* pre;
    vector<int> result;   
    void searchBST(TreeNode* cur) {
    
    
        if (cur == NULL) return ;

        searchBST(cur->left);       // 左
        if (pre == NULL) {
    
     // 第一个节点
            count = 1;
        } else if (pre->val == cur->val) {
    
     // 与前一个节点数值相同
            count++;
        } else {
    
     // 与前一个节点数值不同
            count = 1;
        }
        pre = cur; // 更新上一个节点

        if (count == maxCount) {
    
     // 如果和最大值相同,放进result中
            result.push_back(cur->val);
        }

        if (count > maxCount) {
    
     // 如果计数大于最大值频率
            maxCount = count;   // 更新最大频率
            result.clear();     // 很关键的一步,不要忘记清空result,之前result里的元素都失效了
            result.push_back(cur->val);
        }

        searchBST(cur->right);      // 右
        return ;
    }
    vector<int> findMode(TreeNode* root) {
    
     
        count = 0; 
        maxCount = 0;
        TreeNode* pre = NULL; // 记录前一个节点
        result.clear();
        searchBST(root);
        return result;       
    }
};

236: Common Ancestor of Binary Tree

Insert picture description here
Use backtracking

/**
 * 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:
    TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
    
    
        if(root==p||root==q||root==NULL) return root;
        TreeNode* left=lowestCommonAncestor(root->left,p,q);
        TreeNode* right=lowestCommonAncestor(root->right,p,q);
        if(left!=NULL&&right!=NULL) return root;
        else if(left==NULL&&right!=NULL) return right;
        else if(right==NULL&&left!=NULL) return left;
        else return NULL;
        
    }
};

235: The common ancestor problem of binary search trees

Insert picture description here
Binary search tree, using the nature of size relationship

/**
 * 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:
    TreeNode* lo(TreeNode* node,TreeNode* p,TreeNode* q){
    
    
        if(node==NULL) return node;
        if(node==p||node==q) return node;
        if(node->val>p->val&&node->val<q->val) return node;
        if(node->val<p->val&&node->val<q->val) return lo(node->right,p,q);
        else if(node->val>q->val&&node->val>p->val) return lo(node->left,p,q);
        return NULL;
    }
    TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
    
    
        if(p->val>q->val) return lo(root,q,p);
        else return lo(root,p,q);
        
    }
}; 

701: Insertion operation of binary search tree

Insert picture description here

Use recursion

/**
 * 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* travel(TreeNode* node,int val){
    
    
        if(node==NULL) {
    
    
            TreeNode* node=new TreeNode(val);
            return node;
        }
        if(node->val>val) node->left=travel(node->left,val);
        if(node->val<val) node->right=travel(node->right,val);
        return node;

    }
    TreeNode* insertIntoBST(TreeNode* root, int val) {
    
    
        return travel(root,val);
        
    }
};

450: Delete binary search tree node

Insert picture description here
Need to adjust the structure of the tree and discuss it by situation

/**
 * 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* deleteNode(TreeNode* root, int key) {
    
    
        if(root==NULL) return root;
        if(root->val==key) {
    
    
            if(root->left==NULL)  return root->right;
            else if(root->right==NULL) return root->left;
            else{
    
    
                TreeNode* cur=root->right;
                while(cur->left!=NULL) cur=cur->left;
                cur->left=root->left;
                TreeNode* tmp=root;
                root=root->right;
                delete tmp;
                return root;
            }
        }
        if(root->val>key) root->left=deleteNode(root->left,key);
        if(root->val<key) root->right=deleteNode(root->right,key);    
        return root;    
    }
};

669: Build a binary search tree

Insert picture description here
This question uses recursion, but you must judge the situation clearly and you must not miss it! When I wrote it for the first time, I didn't take into account the fact that the boundary is equal.

/**
 * 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* trimBST(TreeNode* root, int low, int high) {
    
    
        if(root==NULL) return NULL;
        if(root->val>high) return trimBST(root->left,low,high);
        if(root->val<low) return trimBST(root->right,low,high);
        if(root->val>=low&&root->val<=high){
    
    
            root->left=trimBST(root->left,low,high);
            root->right=trimBST(root->right,low,high);;
        }
        return root;
    }
};

108: Convert an ordered array into a binary search tree (construct a binary search tree)

Insert picture description here
When using recursion, you should also pay attention to the boundary conditions and special values. For the first time, the case where nums.size() is 1 is not considered! ! Consider comprehensive

/**
 * 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* sortedArrayToBST(vector<int>& nums) {
    
    
        if(nums.size()==0) return NULL;
        TreeNode* node=new TreeNode(0);
        int index=nums.size()/2;
        node->val=nums[index];
        if(nums.size()==1) return node;
        vector<int> numsl(nums.begin(),nums.begin()+index);
        vector<int> numsr(nums.begin()+index+1,nums.end());
        node->left=sortedArrayToBST(numsl);
        node->right=sortedArrayToBST(numsr);
        return node;
    }
};

It is also necessary to define the vector every time, and then optimize the space usage rate and use the index directly!

538: Convert a binary search tree into an accumulative tree

Insert picture description here
Insert picture description here
The initial idea is to traverse twice, and then take the array and map for storage, but the time and space consumption are relatively large

/**
 * 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 travel(TreeNode* node,vector<int>& re){
    
    
        if(node==NULL) return;
        travel(node->left,re);
        re.push_back(node->val);
        travel(node->right,re);
    }
    void travelt(TreeNode* node,map<int,int>& map){
    
    
        if(node==NULL) return;
        travelt(node->left,map);
        int x=node->val;
        node->val=map[x];
        travelt(node->right,map);
    }
    TreeNode* convertBST(TreeNode* root) {
    
    
        vector<int> re;
        travel(root,re);
        map<int,int> map;
        int ins=0;
        for(int i=re.size()-1;i>=0;i--){
    
    
            ins+=re[i];
            map[re[i]]=ins;
        }
        travelt(root,map);
        return root;
    }
};

Consider replacing directly during a traversal:

/**
 * 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;
    void travel(TreeNode* node){
    
    
        if(node==NULL) return;
        travel(node->right);
        ans+=node->val;
        node->val=ans;
        travel(node->left);

    }
    TreeNode* convertBST(TreeNode* root) {
    
    
        if(root==NULL) return NULL;
        ans=0;
        travel(root);
        return root;
    }
};

Guess you like

Origin blog.csdn.net/weixin_41169280/article/details/114689425