二叉搜索树及公共祖先

530.二叉搜索树的最小绝对差
上一篇文章中的递归法来做,一直记录上一个结点。
这里二叉搜索树一直与中序遍历挂钩

class Solution {
public:
    int result=INT_MAX;
    TreeNode* node=nullptr;
    int getMinimumDifference(TreeNode* root) {
        if(root==nullptr) return 0;
        getMinimumDifference(root->left);
        if(node!=nullptr) {
            result=min(result,root->val-node->val);
        }
        node=root;
        getMinimumDifference(root->right);
        return result;
    }
};

迭代法:
个人更喜欢用统一风格的迭代法解法:

class Solution {
public:
    int getMinimumDifference(TreeNode* root) {
        if(root==nullptr) return 0;
        stack<TreeNode*> st;
        st.push(root);
        int result=INT_MAX;
        TreeNode* pre=nullptr;
        while(!st.empty()) {
            TreeNode* node=st.top();
            if(node!=nullptr) {
                st.pop();
                if(node->right) {
                    st.push(node->right);
                }
                st.push(node);
                st.push(nullptr);
                if(node->left) {
                    st.push(node->left);
                }
            }else{
                st.pop();
                TreeNode* q=st.top();
                if(pre!=nullptr) {
                    result=min(result,q->val-pre->val);
                }
                pre=q;
                st.pop();
            }
        }
        return result;
    }
};

501.二叉搜索树中的众数
用了一次迭代法来解题,细节的地方在于需要一直记录数字出现最大次数,中序遍历可以直接相邻判断就好了,但是循环里面需要判断最后次数结束,循环结束还要判断一次

class Solution {
public:
    vector<int> findMode(TreeNode* root) {
        if(root==nullptr) return vector<int>();
        queue<int> result;
        int max_=0;
        int frequ=0;
        TreeNode* pre=nullptr;
        stack<TreeNode*> st;
        st.push(root);
        while(!st.empty()) {
            TreeNode* node=st.top();
            if(node) {
                st.pop();
                if(node->right) st.push(node->right);
                st.push(node);
                st.push(nullptr);
                if(node->left) st.push(node->left);
            }else{
                st.pop();
                TreeNode* q=st.top();
                if(pre==nullptr) {
                    frequ++;
                }else if(pre->val!=q->val){
                    if(max_==frequ) {
                        result.push(pre->val);
                    }else if(max_<frequ){
                        max_=frequ;
                        while(!result.empty()) {
                            result.pop();
                        }
                        result.push(pre->val);
                    }
                    frequ=1;
                }else{
                    frequ++;
                }
                pre=q;
                st.pop();
            }
        }
        if(max_==frequ) {
            result.push(pre->val);
        }else if(max_<frequ){
            max_=frequ;
            while(!result.empty()) {
                result.pop();
            }
            result.push(pre->val);
        }
        vector<int> res;
        while(!result.empty()) {
            res.push_back(result.front());
            result.pop();
        }
        return res;
    }
};

236.二叉树的最近公共祖先
如果节点是q或者p,可以直接返回;
如果左右分别返回不为nullptr,那么左右必然包含q,p,root直接是最近祖先
如果q是p的祖先,直接返回一方的节点

class Solution {
public:
    TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
         if(root==nullptr) return nullptr;
         if(root==q||root==p) return root;
         TreeNode* left=lowestCommonAncestor(root->left,p,q);
         TreeNode* right=lowestCommonAncestor(root->right,p,q);
         if(left!=nullptr&&right!=nullptr) return root;
         else if(left) return left;
         else if(right) return right;
         else return nullptr;
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值