public Node search(Node root, int key)
        if (root==null || root.key==key)
          return root;
        if (root.key < key)
          return search(root.right, key);
        return search(root.left, key);

      Java language logo
      Search Key in BST


      The code invokes the search() method on the Node object rooted at root and passing in the key as an int. If root is null or the key doesn't exists in the tree, the code returns the root node. If the key is less than root.key, the code returns the search() method on the node rooted at root.left, otherwise the code returns search() on the node rooted at root.right.


        Add Comment

        Log in to add a comment

        Codiga - All rights reserved 2022.