-2

Given the root node of a Binary Search Tree, I'm trying to create a recursive search where all nodes within a given maximum and minimum range are found but in the LEAST amount of visits.

So essentially the set up to this question will be(I think):

public Node finder(Node root, int max, int min) {};

user2251001
  • 51
  • 1
  • 2
  • 1
    "here is the code I have so far, and this is where I get stuck" is usually how this should go. Right now your train of thought hasn't left the station yet... – Floris Dec 15 '13 at 02:11
  • I was thinking that I would just do an if statement to check if both the left and right child were not in range, if so return null (essentially doing nothing) then from there have 2 if statements. 1st if statement: check if the left child is range if so visit it and recursively call the program again using the left child. 2nd if statement: Do the exact same as the first except using the right child. Except i know my logic isn't complete and im missing steps – user2251001 Dec 15 '13 at 06:34

1 Answers1

0

Create a function "check" which is recursively defined as

    check(Node x){
    if(x.right.elm()>min&&x.right.elm()<max){
        check(x.right); 
    }
    else if(x.right.elm()>max){
        check(x.right.left);    
    else if(x.right.elm()<min){
        check(x.right.right);
    }
    if(x.left.elm()>min&&x.left.elm()<max){
        check(x.left); 
    }
    else if(x.left.elm()>max){
        check(x.left.left);    
    else if(x.left.elm()<min){
        check(x.left.right);
    }
    }

Alternatively for a easier code you could create two functions check_right and check_left.

nikhil_vyas
  • 513
  • 5
  • 16