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

how to search for a node in binary tree?

node * search(node *root,int k) {
    if(root == NULL)
      return NULL;
    if(root->data == k)
      return root;
    else {
      search(root->left,k);
      search(root->right,k);
   }
    return NULL;
}

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

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

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);
    }
}