给定以下结构
struct nNode {
int val;
struct nNode parent;
struct nNode children;
struct nNode next;
struct nNode prev;
};
我们需要遵循的children
指向第一个孩子并遍历其他孩子的地方node->children->next
......
val
我正在尝试使用该函数返回一个指向包含一些元素的指针
struct nNode* nNode_find(struct nNode *node, int val)
{
// We found the node return the pointer
if(node->val == val) return node;
// We didn't found let's check the children
struct nNode *child = node->children;
while(child) {
nNode_find(child, val);
child = child->children;
// We didn't found on child, lets check his brothers
struct nNode *sibling = child->next;
while(sibling) {
nNode_find(sibling, val);
sibling = sibling->next;
}
}
// We didn't found the element return NULL
return NULL;
}
给定一个树调用TREE
,如:
/* 1
* /---------|--------\
* 2 3 4
* / \ /
* 5 6 7
*/
类似的命令
struct nNode *ptr = nNode_find(TREE, 3);
应该返回一个指向 的指针root->children->next
,但实际nNode_find
返回的是NULL
。