【发布时间】:2014-05-16 22:49:12
【问题描述】:
#include<stdio.h>
#include<malloc.h>
struct node
{
int data;
struct node* left;
struct node* right;
};
struct node* newNode(int data)
{
struct node* node=(struct node*)malloc(sizeof(struct node));
node->data=data;
node->left=NULL;
node->right=NULL;
return (node);
};
int height(struct node* root)
{
static int lheight,rheight;
if(root==NULL)
return;
else
{
lheight=height(root->left)+1;
rheight=height(root->right)+1;
if(lheight>rheight)
return lheight;
else
return rheight;
}
}
int main()
{
struct node* root=newNode(1);
root->left=newNode(2);
root->right = newNode(3);
root->left->left = newNode(4);
root->left->right = newNode(5);
printf("%d",height(root));
return 0;
}
这个程序给出了两种不同的结果。对于上面我使用静态的程序,一个是 2,如果不使用静态,则为 3。请用static解释输出变化的原因。
【问题讨论】:
-
请在提问前进行搜索。
-
我无法理解这个特定问题的区别。
-
不,不是这样。您希望有人做我们不做的功课。请阅读链接。
-
我已经浏览了链接。我完全明白这意味着什么。但是为什么这个特定代码有两个不同的输出是我无法理解的。
-
我听起来你正在做作业/面试工作..都不应该回答