371两整数之和;501 二叉搜索树中的众数

不使用运算符 + 和 - ​​​​​​​,计算两整数 ​​​​​​​a 、b ​​​​​​​之和。

示例 1:

输入: a = 1, b = 2
输出: 3


示例 2:

输入: a = -2, b = 3
输出: 1

class Solution {
public:
    int getSum(int a, int b) {
        //异或:不进位加法,同为0异为1
        //与+左移1位:进位
        if(!a)return b;
        if(!b)return a;
        //注意leetcode规定int 左移时最高为必须为0,否则左移报错,所以用unsigned
        return getSum(a^b,unsigned(a&b)<<1);
    }
};

给定一个有相同值的二叉搜索树(BST),找出 BST 中的所有众数(出现频率最高的元素)。

假定 BST 有如下定义:


    结点左子树中所含结点的值小于等于当前结点的值
    结点右子树中所含结点的值大于等于当前结点的值
    左子树和右子树都是二叉搜索树

提示:如果众数超过1个,不需考虑输出顺序

进阶:你可以不使用额外的空间吗?(假设由递归产生的隐式调用栈的开销不被计算在内)

class Solution {
public:
    vector<int> findMode(TreeNode* root) {
        if(!root)return {};
        unordered_map<int,int> m;
        int count=0;
        vector<int> v;
        helper(root,m,count);
        for(auto i=m.begin();i!=m.end();++i){
            if(count==i->second)//value
                v.push_back(i->first);//key
        }
        return v;
    }
    void helper(TreeNode* r,unordered_map<int,int>& m,int& c){
        if(r){
            m[r->val]++;
            c=m[r->val]>c?m[r->val]:c;
            helper(r->left,m,c);
            helper(r->right,m,c);
        }
    }
};
    vector<int> findMode(TreeNode* root){
        if(!root)return {};
        TreeNode* pre=nullptr;
        int max=0,count=0;
        vector<int> v;
        helper(root,pre,max,count,v);
        return v;
    }
    void helper(TreeNode* root,TreeNode* &pre,int& max,int& count,vector<int>& v){
        if(root){
            helper(root->left,pre,max,count,v);
            if(pre&&pre->val==root->val)//和前面一个元素相同的元素的处理方式
                ++count;                
            else
                count=1;//第一个元素和 和前面一个元素不同的元素 的处理方式
            if(count>max){
                max=count;
                v.clear();
                v.push_back(root->val);               
            }
            else if(count==max)
                v.push_back(root->val);
            pre=root;
            helper(root->right,pre,max,count,v);
        }
    }

morris中序遍历优化,时间复杂度O(n),空间复杂度O(1)

class Solution {
public:
    vector<int> findMode(TreeNode* root){
        TreeNode *cur=root,*pre=nullptr,*temp=nullptr;
        int count=0;
        int max=0;
        vector<int> v;
        while(cur){
            temp=cur->left;
            if(temp){
                while(temp->right&&temp->right!=cur)
                    temp=temp->right;
                if(temp->right)
                    temp->right=nullptr;
                else{
                    temp->right=cur;
                    cur=cur->left;
                    continue;
                }
            }
            //处理start
            if(pre&&pre->val==cur->val)
                ++count;                
            else
                count=1;
            if(count>max){
                max=count;
                v.clear();
                v.push_back(cur->val);               
            }
            else if(count==max)
                v.push_back(cur->val);
            //处理end
            pre=cur;
            cur=cur->right;
        }
        return v;
    }

morris前中序遍历代码对比如下,直接记忆背诵,方便使用

void InOrder(TreeNode* root){//morris中序(左,根,右)
    TreeNode *cur=root,*pre=nullptr,*temp=nullptr;
    while(cur){
        temp=cur->left;
        if(temp){
            while(temp->right&&temp->right!=cur)
                temp=temp->right;
            if(temp->right)
                temp->right=nullptr;
            else{
                temp->right=cur;
                cur=cur->left;
                continue;
            }
        }
	operate(cur),pre=cur;//如果需要用到前驱节点则需定义pre,否则不用
        cur=cur->right;
    }
}
void PreOrder(TreeNode* root){//morris前序(根,左,右)
    TreeNode *cur=root,*pre=nullptr,*temp=nullptr;
    while(cur){
        temp=cur->left;
        if(temp){
            while(temp->right&&temp->right!=cur)
                temp=temp->right;
            if(temp->right)
                temp->right=nullptr;
            else{
                temp->right=cur;
                operate(cur),pre=cur;//如果需要用到前驱节点则需定义pre,否则不用
                cur=cur->left;
                continue;
            }
        }
        else
	    operate(cur),pre=cur;//如果需要用到前驱节点则需定义pre,否则不用
        cur=cur->right;
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值