如何在二叉树中搜索节点?

时间:2016-01-26 17:00:26

标签: c tree binary-tree

NSString

我不知道为什么这个功能不起作用?请帮助。

1 个答案:

答案 0 :(得分:1)

如果找到节点,则不会返回递归调用的结果。

node * search(node *root,int k) {
    if(root == NULL) {
      return NULL;
    } else if (root->data == k) {
      return root;
    } else {
      node* x = search(root->left,k);
      if (x)
        return x;         //if we find in left subtree, return result
      return search(root->right,k);
    }
}