这串代码是不能的,但老师后面进行了修改,就是用您所说的递归方法,请您把视频再看一遍。
我也发现了
加入坐标是5,老师用根节点去掉用,只能找0,1,2. 的节点 根本就没有找到5节点就返回NULL了 。 我勒个去
谢谢分享
后来改了,前面的是有错,没考虑周全
Node *Node::searchNode(int nodeIndex)
{
Node *temp=NULL;
if(this->index==nodeIndex) return this;
if(this->pLChild!=NULL)
{
if(this->pLChild->index==nodeIndex)
return this->pLChild;
else
{
temp=this->pLChild->searchNode(nodeIndex);
if(temp!=NULL) return temp;
}
}
if(this->pRChild!=NULL)
{
if(this->pRChild->index==nodeIndex)
return this->pRChild;
else
{
temp=this->pRChild->searchNode(nodeIndex);
if(temp!=NULL) return temp;
}
}
return NULL;
}
寻找节点函数需要返回一个节点对象,就定义成为了节点的指针。
111
bbb
这是伏笔,第6-6节会点破这个bug,不过子节点判断两次这个,倒没改过来。