LeetCode LCA、BST

530. 二叉搜索树的最小绝对差

方法:递归

/**
 * 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 {
private:
    vector<int> res;
    void solve(TreeNode* cur) {
        if (cur == NULL) return ;
        solve(cur->left);
        res.push_back(cur->val);
        solve(cur->right);
    }
public:
    int getMinimumDifference(TreeNode* root) {
        solve(root);
        int n = res.size(), minv = 1e5+10;

        for (int i = 1; i < n; ++i) {
            if (res[i] - res[i-1] < minv) minv = res[i] - res[i-1];
        }
        return minv;
    }
};

$时间复杂度O(n),空间复杂度O(n)

方法:迭代

/**
 * 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 getMinimumDifference(TreeNode* root) {
        stack<TreeNode*> st;
        TreeNode* cur = root;
        TreeNode* pre = NULL;
        int minv = 1e5+10;
        while (cur != NULL || !st.empty()) {
            if (cur != NULL) {
                st.push(cur);
                cur = cur->left;
            } else {
                cur = st.top();
                st.pop();
                if (pre != NULL) {
                    minv = min(minv, cur->val - pre->val);
                }
                pre = cur;
                cur = cur->right;
            }
        }
        return minv;
    }
};

$时间复杂度O(n),空间复杂度O(n)

501. 二叉搜索树中的众数

方法:递归

/**
 * 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 {
private:
    vector<int> res;
    TreeNode* pre = NULL;
    int maxv = 0, cnt = 0; 
    void solve(TreeNode* cur) {
        if (cur == NULL) return ;
        solve(cur->left);
        if (pre == NULL) cnt = 1;
        else if (cur->val == pre->val) ++cnt;
        else cnt = 1;
        
        if (cnt == maxv) res.push_back(cur->val);
        if (cnt > maxv) {
            res.clear();
            maxv = cnt;
            res.push_back(cur->val);
        }
        pre = cur;
        solve(cur->right);
        return ;
    }
public:
    vector<int> findMode(TreeNode* root) {
        solve(root);
        return res;
    }
};

$时间复杂度O(n),空间复杂度O(n)

方法:迭代

/**
 * 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> findMode(TreeNode* root) {
        int cnt = 0, maxv = 0;
        stack<TreeNode*> stk;
        TreeNode* cur = root;
        TreeNode* pre = NULL;
        vector<int> res;
        while (cur != NULL || !stk.empty()) {
            if (cur != NULL) {
                stk.push(cur);
                cur = cur->left;
            } else {
                cur = stk.top();
                stk.pop();
                if (pre == NULL) cnt = 1;
                else if (pre->val == cur->val) ++cnt;
                else cnt = 1;
                if (cnt == maxv) res.push_back(cur->val);
                if (cnt > maxv) {
                    maxv = cnt;
                    res.clear();
                    res.push_back(cur->val);
                }
                pre = cur;
                cur = cur->right;
            }
        }
        return res;
    }
};

$时间复杂度O(n),空间复杂度O(n)

236. 二叉树的最近公共祖先

方法:递归

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

$时间复杂度O(n),空间复杂度O(n)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值