按顺序遍历递归循环

时间:2015-06-17 03:24:20

标签: java recursion binary-search-tree

我已经搜索了SOF一段时间并阅读了类似的帖子。我没有发现任何令人满意的答案。我遇到的问题是我想遍历一棵树,直到'根节点'和目标节点相同。如果'root'== target,我想返回true,否则为false。

这就是我所拥有的

DateTime

你会注意到注释掉了false并返回true。如果我交换返回,我能够让程序在所有测试用例中正常运行。但是,我个人不喜欢这个解决方案。我添加了print语句来跟踪问题,我使用Netbeans作为我的调试器。当我运行代码时,它确实显示它正在进入

private boolean modifiedInorderTraversal(Node newRoot, Node target){     
        if(newRoot.leftPointer != null){
            modifiedInorderTraversal(newRoot.leftPointer, target);
        }

        if(newRoot == target){
            System.out.println("newRoot == target");
            return true;
            //return false;
        } 

        if(newRoot.rightPointer != null){
            modifiedInorderTraversal(newRoot.rightPointer, target);
        }

        System.out.println("before false return");
    //return true;   
        return false; 
    }

我知道这是因为我的线条是打印的。现在,看起来我的递归仍然继续,尽管命中return语句为true,导致false返回false。我尝试使用全局变量来阻止递归无济于事。 该尝试的示例

if(newRoot == target){
    System.out.println("newRoot == target");
    return true;
    //return false;
} 

有什么想法吗?我已经读过,可以使用一个例外来打破循环,但我认为这也很混乱 *********************** UPDATE ********************* 我发现这种方式与我期望的方式相同

private breakRecursion = false;

private boolean example(Node newRoot, Node target){
    if(!breakRecursion){
        if(blah blah){recurse}
        if(newRoot == target){breakRecursion = true, return true}
        if(blah blah){recurse}
        return false;
    }
}

我对此解决方案的唯一问题是运行时是O(n)。最糟糕的情况应该是O(n),但是这个解决方案不会提前中断并遍历所有节点,无论目标是在哪里找到。

1 个答案:

答案 0 :(得分:1)

您的问题是,当您从递归堆栈中的一个级别返回指示您找到目标时,您忘记在较高级别使用此信息来停止搜索。试试这个(当然,删除最终产品中的打印陈述):

private static boolean modifiedInorderTraversal(Node newRoot, Node target){     
    boolean foundIt = false;

    if(newRoot.leftPointer != null){
        foundIt = modifiedInorderTraversal(newRoot.leftPointer, target);
        if(foundIt){
            System.out.println("Target found in left subtree");
            return true;
        }
    }

    System.out.println("searching: "+newRoot.value);
    if(newRoot == target){
        System.out.println("Target found at current node!");
        return true; //the target is this node!
    } 

    if(newRoot.rightPointer != null){
        foundIt = modifiedInorderTraversal(newRoot.rightPointer, target);
        if(foundIt){
            System.out.println("Target found in right subtree!");
            return true;
        }
    }

    System.out.println("Not found below root "+newRoot.value);
    return false; //the target is in neither subtree, and is not the root :-(
}

//My tree:
//      a
//    /   \
//    b    c
//   /\    /\
//  d e   f  g
public static void main(String[] arg){
    Node d = new Node(null,null,"d");
    Node e = new Node(null,null,"e");
    Node f = new Node(null,null,"f");
    Node g = new Node(null,null,"g");

    Node b = new Node(d,e,"b");
    Node c = new Node(f,g,"c");

    Node a = new Node(b,c,"a");

    System.out.println("Searching for f");
    if(modifiedInorderTraversal(a, f)){
        System.out.println("Success!");
    }

    System.out.println("---------------------------------");

    System.out.println("Searching for q");
    Node q = new Node(null,null,"q");
    if(modifiedInorderTraversal(a, q)){
        System.out.println("Shouldn't make it here...");
    } else{
        System.out.println("Didn't find q, as we shouldn't");
    }
}

private static class Node {
    public Node(Node left, Node right, String value){
        leftPointer = left;
        rightPointer = right;
        this.value = value;
    }
    Node leftPointer;
    Node rightPointer;
    String value;
}

如果在左子树,当前节点或右子树中找到该值,则将停止搜索。如果在任何一个目标中找不到目标,那么我们就放弃了。运行上面的内容给出了:

Searching for f
searching: d
Not found below root d
searching: b
searching: e
Not found below root e
Not found below root b
searching: a
searching: f
Target found at current node!
Target found in left subtree
Target found in right subtree!
Success!
---------------------------------
Searching for q
searching: d
Not found below root d
searching: b
searching: e
Not found below root e
Not found below root b
searching: a
searching: f
Not found below root f
searching: c
searching: g
Not found below root g
Not found below root c
Not found below root a
Didn't find q, as we shouldn't