/** Initialize your data structure here. */
queue<int> q;
int sz=0;
double sum=0;
MovingAverage(int size) {
sz=size;
}
double next(int val) {
if(q.size()==sz){
sum-=q.front();
q.pop();
sum+=val;
q.push(val);
return sum/q.size();
}else {
q.push(val);
sum+=val;
return sum/q.size();
}
}
vector<int> vec;
RecentCounter() {
}
int ping(int t) {
vec.push_back(t);
int res=0;
for(int i=vec.size()-1;i>=0;i--)
if(vec[i]>=t-3000) res++;
else break;
return res;
}
TreeNode* tree;
vector<TreeNode*>v;
CBTInserter(TreeNode* root) {
tree=root;
queue<TreeNode*> q;
if(root!=NULL){
q.push(tree);
while(q.size()){
auto cc=q.front();
v.push_back(cc);
q.pop();
if(cc->left) q.push(cc->left);
if(cc->right) q.push(cc->right);
}
}
// for(int i=0;i<v.size();i++) cout<<v[i]->val<<" ";
// cout<<endl;
}
int insert(int x) {
TreeNode* cc= new TreeNode();
cc->val=x;
if(v.size()%2==0){
TreeNode* fa=v[v.size()/2-1];
fa->right=cc;
v.push_back(cc);
return fa->val;
} else{
TreeNode* fa=v[v.size()/2];
fa->left=cc;
v.push_back(cc);
return fa->val;
}
}
TreeNode* get_root() {
return tree;
}
vector<int> largestValues(TreeNode* root) {
vector<int> res;
queue<TreeNode*>q;
if(root==NULL) return res;
else q.push(root);
while(q.size()){
int xx=INT_MIN;
int len=q.size();
for(int i=0;i<len;i++){
auto cc=q.front();
q.pop();
if(cc->left) q.push(cc->left);
if(cc->right) q.push(cc->right);
xx=max(xx,cc->val);
}
res.push_back(xx);
}
return res;
}
int res=0,h=0;
int findBottomLeftValue(TreeNode* root) {
dfs(root,1);
return res;
}
void dfs(TreeNode* root,int height){
if(root==NULL) return;
dfs(root->left,height+1);
dfs(root->right,height+1);
// cout<<root->val<<" "<<height<<endl;
if(height>h) res=root->val,h=height;
}
vector<int> res;
vector<int> rightSideView(TreeNode* root) {
queue<TreeNode*> q;
if(root==NULL) return res;
else q.push(root);
while(q.size()){
int len=q.size();
for(int i=0;i<len;i++){
auto cc=q.front();
q.pop();
if(cc->left) q.push(cc->left);
if(cc->right) q.push(cc->right);
if(i==len-1) res.push_back(cc->val);
}
}
return res;
}
TreeNode* pruneTree(TreeNode* root) {
xx(root);
return root;
}
void xx(TreeNode* root){
if(root==NULL) return ;
xx(root->left);
xx(root->right);
if(root->val==0&&!root->left&&!root->right) root=NULL;
}
// Encodes a tree to a single string.
string serialize(TreeNode* root) {
string res;
dfs_s(root,res);
// cout<<res<<endl;
return res;
}
void dfs_s(TreeNode* root,string& res){
if(root==NULL) {
res+="#,";
return ;
}
res+=to_string(root->val)+',';
dfs_s(root->left,res);
dfs_s(root->right,res);
}
// Decodes your encoded data to tree.
TreeNode* deserialize(string data) {
int idx=0;
return dfs_d(idx,data);
}
TreeNode* dfs_d(int& idx,string res){
if(res[idx]=='#'){
idx+=2;
return NULL;
}
auto cc=new TreeNode();
int oriIdx=idx;
while(res[idx]!=',') idx++;
cc->val=atoi(res.substr(oriIdx,idx-oriIdx).c_str());
idx++;
cc->left=dfs_d(idx,res);
cc->right=dfs_d(idx,res);
return cc;
}
剑指 Offer II 049. 从根节点到叶节点的路径数字之和
int sum=0;
int sumNumbers(TreeNode* root) {
string str;
dfs(root,str);
return sum;
}
void dfs(TreeNode* root,string str){
if(root&&!root->left&&!root->right) {
str+=to_string(root->val);
sum+=atoi(str.c_str());
return ;
}
if(root==NULL){
return ;
}
str+=to_string(root->val);
dfs(root->left,str);
dfs(root->right,str);
}
int res=0;
int pathSum(TreeNode* root, int targetSum) {
dfs(root,targetSum);
return res;
}
void dfs(TreeNode* root,int targetSum){
if(root==NULL) return ;
dfs_s(root,targetSum,0);
dfs(root->left,targetSum);
dfs(root->right,targetSum);
}
void dfs_s(TreeNode* root,int targetSum,long s){
if(root==NULL) return ;
s+=root->val;
if(s==targetSum) res++;
dfs_s(root->left,targetSum,s);
dfs_s(root->right,targetSum,s);
}
vector<int> vec;
TreeNode* increasingBST(TreeNode* root) {
dfs(root);
return xx(0);
}
void dfs(TreeNode* tree){
if(!tree) return;
dfs(tree->left);
vec.push_back(tree->val);
dfs(tree->right);
}
TreeNode* xx(int idx){
if(idx>=vec.size()) return NULL;
auto cc=new TreeNode(vec[idx]);
cc->left=NULL;
cc->right=xx(idx+1);
return cc;
}
int xx=0;
TreeNode* res;
TreeNode* inorderSuccessor(TreeNode* root, TreeNode* p) {
xx=p->val;
int st=0;
dfs(root,xx,st);
return res;
}
void dfs(TreeNode* root,int xx,int& st){
if(st==2) return ;
if(root==NULL) return ;
dfs(root->left,xx,st);
if(st==1){
res=new TreeNode(root->val);
st=2;
}
if(root->val==xx) st=1;
dfs(root->right,xx,st);
}
int res=INT_MIN;
int maxPathSum(TreeNode* root) {
dfs(root);
return res;
}
int dfs(TreeNode* root){
if(root&&!root->left&&!root->right){
res=max(res,root->val);
return root->val;
}
if(root==NULL) return 0;
int le=dfs(root->left);
int re=dfs(root->right);
int sum=le+re+root->val;
// cout<<le<<" "<<re<<" "<<sum<<endl;
if(res<le&&le==0&&!root->left){
int xx=max(max(sum,root->val),max(re,re+root->val));
res=max(res,xx);
}
else if(res<re&&re==0&&!root->right){
int xx=max(max(sum,root->val),max(le,le+root->val));
res=max(res,xx);
}
else{
int xx=max(res, max(max( max(le,re),max(sum,root->val)),max(le,re)+root->val ));
res=max(res,xx);
}
return max(max(le,re)+root->val,root->val);
}
int sum=0;
TreeNode* convertBST(TreeNode* root) {
dfs(root);
return root;
}
void dfs(TreeNode* root){
if(root==NULL) return ;
dfs(root->right) ;
int ori=root->val;
root->val+=sum;
sum+=ori;
dfs(root->left);
}
vector<int> res;
int idx=0;
BSTIterator(TreeNode* root) {
res.push_back(-1);
dfs(root);
}
void dfs(TreeNode* root){
if(!root) return ;
dfs(root->left);
res.push_back(root->val);
dfs(root->right);
}
int next() {
return res[++idx];
}
bool hasNext() {
if(idx<res.size()-1)return true;
else return false;
}
vector<int> res;
bool findTarget(TreeNode* root, int k) {
dfs(root);
for(int i=0,j=res.size()-1;i<j;i++){
while(i<j&&res[i]+res[j]>k) j--;
if(i<j&&res[i]+res[j]==k){
return true;
}
}
return false;
}
void dfs(TreeNode* tree){
if(!tree) return ;
dfs(tree->left);
res.push_back(tree->val);
dfs(tree->right);
}
剑指 Offer II 057. 值和下标之差都在给定的范围内
bool containsNearbyAlmostDuplicate(vector<int>& nums, int k, int t) {
typedef long long LL;
multiset<LL> S;
S.insert(1e18);
S.insert(-1e18);
for(int i=0,j=0;i<nums.size();i++){
while(i-j>k) S.erase(S.find(nums[j++]));
auto it= S.lower_bound(nums[i]);
if(*it-nums[i]<=t) return true;
it--;
if(nums[i]-*it<=t) return true;
S.insert(nums[i]);
}
return false;
}
vector<vector<int>> res;
MyCalendar() {
}
bool book(int start, int end) {
for(int i=0;i<res.size();i++){
if(start<=res[i][0]&&end>res[i][0]) return false;
if(start<res[i][1]&&end>=res[i][1]) return false;
if(start>=res[i][0]&&end<=res[i][1]) return false;
}
vector<int> xx;
xx.push_back(start),xx.push_back(end);
res.push_back(xx);
return true;
}
int kk=0;
multiset<int> nums;
KthLargest(int k, vector<int>& xx) {
kk=k;
for(int i=0;i<xx.size();i++) nums.insert(xx[i]);
}
int add(int val) {
nums.insert(val);
int xx=nums.size()-kk;
auto it=nums.begin();
while(xx--) it++;
return *it;
}
剑指 Offer II 060. 出现频率最高的 k 个数字
static bool cmp(const pair<int,int>& a, const pair<int,int >& b ){
return a.second>b.second;
}
vector<int> topKFrequent(vector<int>& nums, int k) {
unordered_map<int,int> map;
for(int i=0;i<nums.size();i++) map[nums[i]]++;
vector<pair<int,int>> xx(map.begin(),map.end());
vector<int> res;
sort(xx.begin(),xx.end(),cmp);
int cnt=0;
for(auto cc:xx){
res.push_back(cc.first);
cnt++;
if(cnt==k) return res;
}
return res;
}