此計算垂直總和是我碰到其計算二叉樹垂直和代碼。由於該代碼根本沒有任何文檔,我無法理解它是如何實際工作的,以及if(base == hd)的條件是什麼? 需要幫助:)二叉樹
void vertical_line(int base,int hd,struct node * node)
{
if(!node) return;
vertical_line(base-1,hd,node->left);
if(base==hd) cout<<node->data<<" ";
vertical_line(base+1,hd,node->right);
}
void vertical_sum(struct node * node)
{
int l=0,r=0;
struct node * temp=node;
while(temp->left){
--l;temp=temp->left;
}
temp=node;
while(temp->right){
++r;temp=temp->right;
}
for(int i=l;i<=r;i++)
{
cout<<endl<<"VERTICAL LINE "<<i-l+1<<" : ";
vertical_line(0,i,node);
}
}
http://stackoverflow.com/q/9646575/335858 – dasblinkenlight