这是我试图解决问题中提到的问题的代码:
int getHeight(struct node* root) { // Write your code here
int ldepth=0;
int rdepth=0;
if(root==NULL)
return 0;
else{
ldepth=getHeight(root->left);
rdepth=getHeight(root->right);
if(ldepth>rdepth)
return ldepth+1;
else (rdepth>ldepth)
return rdepth+1;
return 0;
}
}
答案 0 :(得分:0)
如果ldepth==rdepth
,则返回0,无论其大小如何:
int getHeight(struct node* root) { // Write your code here
int ldepth;
int rdepth;
if(root==NULL)
return 0;
else{
ldepth = 1 + getHeight(root->left);
rdepth = 1 + getHeight(root->right);
return (ldepth > rdepth) ? ldepth : rdepth;
}
}