It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center
                            
                        
                    
                
                                6年前关闭。
            
                    
TreeNode search(int value, TreeNode root)
{

    if(root.data==value)
    {
        return root;
    }
    else if(root.data < value)
    {
        search(value, root.Right);
    }
    else if (root.data > value)
    {
        search(value, root.Left);
    }

   return root;

}


我想在BST中搜索一个节点,问题是此函数返回了几次。它确实返回正确的Node,但最终返回Null。帮我改善它。

最佳答案

更改

    search(value, root.Right);




    return search(value, root.Right);


(也适用于root.Left)。

否则,您将忽略递归调用的返回值。

关于java - BST中的递归搜索,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/16251783/

10-13 06:55