Leetcode 二叉树 501 236 235 701 450

501. Find Mode in Binary Search Tree

class Solution {
private:
    vector num;
    void traversal(TreeNode* root){
        if(root == NULL) return;
        traversal(root->left);
        num.push_back(root->val);
        traversal(root->right);
    }
public:
    vector findMode(TreeNode* root) {
        traversal(root);
        if(num.size() == 1) return vector{num[0]};
        vector res;
        res.push_back(num[0]);
        int maxNum = 1;
        int count = 1;
        for(int i=1; i= maxNum){
                if(count > maxNum){
                    res.clear();
                }
                maxNum = count;
                res.push_back(num[i]);
            }
        }
        return res;
    }
};

用unordered_map(不是搜索二叉树也可以用)

class Solution {
private:
    unordered_map map;
    void traversal(TreeNode* root){
        if(root == NULL) return;
        traversal(root->left);
        map[root->val]++;
        traversal(root->right);
    }
    bool static cmp(const pair& a, const pair b){
        return a.second > b.second;
    } // 从大到小
public:
    vector findMode(TreeNode* root) {
        traversal(root);
        vector res;
        vector> vec(map.begin(), map.end());
        sort(vec.begin(), vec.end(), cmp);
        res.push_back(vec[0].first);
        for(int i=1; i

236. Lowest Common Ancestor of a Binary Tree

class Solution {
public:
    TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
        if(root == NULL) return NULL;
        if(root == p || root == q) return root;

        TreeNode* left = lowestCommonAncestor(root->left, p, q);
        TreeNode* right = lowestCommonAncestor(root->right, p, q);

        if(left != NULL && right != NULL) return root;
        if(left == NULL && right == NULL) return NULL;
        return left == NULL ? right:left;
    }
};

这道题要从最下面开始找,当p,q分别在左右两端的时候证明此时就为公共祖先

 235. Lowest Common Ancestor of a Binary Search Tree

class Solution {
public:
    TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
        if(root == NULL) return NULL;
        if(root == p || root == q) return root;
        if(p->val < q->val){
            swap(p, q);
        }
        if(root->val < p->val && root->val > q->val) return root;
        else if(root->val > p->val) return lowestCommonAncestor(root->left, p, q);
        else return lowestCommonAncestor(root->right, p, q);
    }
};

因为可以利用bst的特点,所以从上往下找就可以了

701. Insert into a Binary Search Tree

1.递归

class Solution {
public:
    TreeNode* insertIntoBST(TreeNode* root, int val) {
        if(root == NULL){
            TreeNode* newNode = new TreeNode(val);
            return newNode;
        }

        if(root->val > val) root->left = insertIntoBST(root->left, val);
        if(root->val < val) root->right = insertIntoBST(root->right, val);

        return root;
        
    }
};

2.迭代

class Solution {
public:
    TreeNode* insertIntoBST(TreeNode* root, int val) {
        if(root == NULL){
            TreeNode* newNode = new TreeNode(val);
            return newNode;
        }

        TreeNode* cur = root;
        TreeNode* parent = root;
        while(cur != NULL){
            parent = cur;
            if(cur->val > val) cur = cur->left;
            else cur = cur->right;
        }
        TreeNode* node = new TreeNode(val);
        if(parent->val > val) parent->left = node;
        else parent->right = node;
        return root;
        
    }
};

450. Delete Node in a BST

class Solution {
private:
    TreeNode* getMin(TreeNode* root){
        while(root->left != NULL) root = root->left;
        return root;
    }
public:
    TreeNode* deleteNode(TreeNode* root, int key) {
        if(root == NULL) return root;

        if(root->val > key) root->left = deleteNode(root->left, key);
        if(root->val < key) root->right = deleteNode(root->right, key);
        if(root->val == key){
            if(root->left == NULL) return root->right;
            if(root->right == NULL) return root->left;
            if(root->left != NULL && root->right != NULL){
                TreeNode* node = getMin(root->right);
                root->right = deleteNode(root->right, node->val);
                node->left = root->left;
                node->right = root->right;
                root = node;
            } 
        } 

        return root;
    }
};

遇到删除的节点,一共会有三种情况

1.左右都没有节点,直接删掉

2.有一个节点, 用这个节点取代原来的节点

3.左右都有节点,要用左子树的最大值,或者右子树的最小值替换

注意

1.第三种情况在替换的时候,要记得删除掉原本右子树最小的值

2.因为这里要返回root, 所以root的值不能改变

你可能感兴趣的:(leetcode,算法)