代码随想录第18天|二叉树续

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 {
public:
    vector<int> path;
    void getInPath(TreeNode* cur) {
        if (cur == nullptr)
            return;
        getInPath(cur->left);
        path.push_back(cur->val);
        getInPath(cur->right);
    }
    int getMinimumDifference(TreeNode* root) {
        getInPath(root);
        int minn = INT_MAX;
        for (int i = 1; i < path.size(); i++) {
            minn = min(minn, path[i] - path[i - 1]);
        }
        return minn;
    }
};

递归(记录上一个的指针)

/**
 * 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* pre = nullptr;
    int res = INT_MAX;
    void search(TreeNode* cur) {
        if (cur == nullptr)
            return;
        search(cur->left);
        if (pre != nullptr) {
            res = min(res, cur->val - pre->val);
        }
        pre = cur;
        search(cur->right);
    }
    int getMinimumDifference(TreeNode* root) {
        search(root);
        return res;
    }
};

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 {
public:
    vector<int> path;
    void search(TreeNode* cur) {
        if (cur == nullptr)
            return;
        search(cur->left);
        path.push_back(cur->val);
        search(cur->right);
    }
    vector<int> findMode(TreeNode* root) {
        search(root);
        int count = 0;
        int maxcount = 0;
        int i, j;
        vector<int> res;
        for (i = 0; i < path.size();) {
            j = i;
            while (j < path.size() && path[j] == path[i])
                j++;
            count = j - i;
            if (count == maxcount)
                res.push_back(path[i]);
            if (count > maxcount) {
                res.clear();
                res.push_back(path[i]);
                maxcount = count;
            }
            i = j;
        }
        return res;
    }
};

下面的代码适合于任何二叉树

/**
 * 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:
    unordered_map<int, int> mp;
    void search(TreeNode* cur) {
        if (cur == nullptr)
            return;
        search(cur->left);
        mp[cur->val]++;
        search(cur->right);
    }
    vector<int> findMode(TreeNode* root) {
        search(root);
        vector<pair<int, int>> r(mp.begin(), mp.end());
        sort(r.begin(), r.end(),
             [](const pair<int, int>& a, const pair<int, int>& b) {
                 return a.second > b.second;
             });
        vector<int> res;
        res.push_back(r[0].first);
        for (int i = 1; i < r.size(); i++) {
            if (r[i].second == r[0].second)
                res.push_back(r[i].first);
        }
        return res;
    }
};

遍历肯定是:中序遍历,这样遍历出的节点是递增的
使用一个指向上一个节点的指针pre,将当前节点和上一个节点进行比较来进行相同值的记数
如果当前数的数量等于最大数量,则将其放入结果数组
当然之后该数的数量可能还会增加,因为还没有遍历完,所以我们再判断,如果当前数的数量大于最大数量,则将结果数组清空,将该数放入结果数组

/**
 * 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 count = 0;
    int maxcount = 0;
    TreeNode* pre = nullptr;
    vector<int> res;
    void search(TreeNode* cur) {
        if (cur == nullptr)
            return;
        search(cur->left);
        if (pre == nullptr) {
            count = 1;
        } else if (pre->val == cur->val) {
            count++;
        } else {
            count = 1;
        }
        if (count == maxcount) {
            res.push_back(cur->val);
        }
        if (count > maxcount) {
            maxcount = count;
            res.clear();
            res.push_back(cur->val);
        }
        pre = cur;
        search(cur->right);
        return;
    }
    vector<int> findMode(TreeNode* root) {
        search(root);
        return res;
    }
};

236. 二叉树的最近公共祖先(重要)

在这里插入图片描述
在这里插入图片描述

第一种情况:某个非p非q的节点是他们俩的公共祖先,只需要寻找左子树和右子树里是否存在p和q,如果有则返回即可。
第二种情况:p是q的祖先或q是p的祖先,这种情况再代码中可以一起和第一种情况实现。
在这里插入图片描述

/**
 * 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 NULL;
        // 返回的是p或q的某个祖先,但不是公共祖先,在某层递归里,如果左右递归都有返回值,那么返回的是root:即公共祖先
        // 可以用返回left/right传给上一层结果,告诉上层函数已经找到了某个节点。
        else if(left != NULL && right ==NULL) return left;
        else if(left == NULL && right != NULL) return right;
        // 返回公共祖先
        else return root;
    }
};
  • 5
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值