代码未在在线编译器上显示结果
code not showing result on online compiler
我正在尝试为树的垂直顺序遍历编写代码,我的代码在 code::blocks 上打印结果但是当同样的事情在 geekforgeeks 运行 上时它没有打印结果在线 ide。为什么要这样做?
void getvert(Node* root,int hd,map<int,vector<int>>m){
if(root==NULL)return;
m[hd].push_back(root->data);
getvert(root->left,hd-1,m);
getvert(root->right,hd+1,m);
}
void verticalOrder(Node *root)
{
map<int,vector<int>>m;
int hd=0;
getvert(root,hd,m);
auto it=m.begin();
for(;it!=m.end();it++)
{
for (int i=0; i<it->second.size(); ++i)
cout<<it->second[i];
cout<<endl;
}
}
函数 getvert
接受最后一个参数 m
作为值。在函数中对它所做的更改是对该对象的本地副本进行的。因此,您在 verticalOrder
.
中看不到任何变化
更改 getvert
使其接受 m
作为参考。
void getvert(Node* root,int hd, map<int,vector<int>>& m) // Need reference argument
{
...
}
我正在尝试为树的垂直顺序遍历编写代码,我的代码在 code::blocks 上打印结果但是当同样的事情在 geekforgeeks 运行 上时它没有打印结果在线 ide。为什么要这样做?
void getvert(Node* root,int hd,map<int,vector<int>>m){
if(root==NULL)return;
m[hd].push_back(root->data);
getvert(root->left,hd-1,m);
getvert(root->right,hd+1,m);
}
void verticalOrder(Node *root)
{
map<int,vector<int>>m;
int hd=0;
getvert(root,hd,m);
auto it=m.begin();
for(;it!=m.end();it++)
{
for (int i=0; i<it->second.size(); ++i)
cout<<it->second[i];
cout<<endl;
}
}
函数 getvert
接受最后一个参数 m
作为值。在函数中对它所做的更改是对该对象的本地副本进行的。因此,您在 verticalOrder
.
更改 getvert
使其接受 m
作为参考。
void getvert(Node* root,int hd, map<int,vector<int>>& m) // Need reference argument
{
...
}