为什么我的递归不起作用? (JAVA)

时间:2014-07-21 08:40:15

标签: java algorithm recursion tree

我想找到二叉树的最低Commone Ancestor(不是二叉搜索树!)为此,我使用此网页中的第二种方法:http://www.geeksforgeeks.org/lowest-common-ancestor-binary-tree-set-1/ 基本上,在Java中,我的方法如下所示:

  private static int LCA2(Node root, Node n1, Node n2)
  {
    if(root == null) return -1;

    if(root.id == n1.id || root.id == n2.id) return root.id;

    int left = LCA2(root.left, n1, n2);
    int right = LCA2(root.right, n1, n2);

    if(left != -1 && right != -1) return root.id;

    if(left != -1) return LCA2(root.left, n1, n2);

    return LCA2(root.right, n1, n2);
}

这是main()函数:

   public static void main (String[] args)
   {
        List<Node> tree = new ArrayList<Node>();

        Node n1 = new Node(1);
        Node n2 = new Node(2);
        Node n3 = new Node(3);
        Node n4 = new Node(4);
        Node n5 = new Node(5);
        Node n6 = new Node(6);
        Node n7 = new Node(7);
        Node n8 = new Node(8);
        Node n9 = new Node(9);

        n1.left = n2;
        n1.right = n3;
        n2.left = n4;
        n2.right = n5;
        n5.left = n6;
        n3.left = n8;
        n3.right = n7;
        n8.right = n9;

        res = new ArrayList<Integer>();
        int k = 2;

//      findKNodes(n1, k);
//      for (int i = 0; i < res.size(); i++)
//          System.out.print(res.get(i) + " ");

        int res = LCA2(n1, n4, n6);

        System.out.println(res);

    }

基本上我的树看起来像这样:

    1
   / \
  2   3
 / \  /\
4  5  8 7
  /   \
 6     9

我运行递归函数LCA(Node root, node n1, node n2),其中root = 1,n1 = 4,n2 = 6;因此,在LCA的第一次递归调用之后,我希望结果返回left = 4right = -1并递归到根的左子树上。然而,它返回left = 6right = -1,这对于第一次迭代来说不是问题,但是对于下一次迭代,它会进入无限循环,我不知道如何解决这个问题。

编辑:类Node的代码:

public static class Node
{
    int id;
    Node left;
    Node right;

    public Node (int id)
    {
        this.id = id;
    }
}

2 个答案:

答案 0 :(得分:2)

注意:在解决了重新编译代码的问题后(​​我在评论中提到过),这里有一个提示,可以帮助您的代码更快地运行。

为避免两次调用相同的方法LCA2,您应该按如下方式重写您的方法:

private static int LCA2(Node root, Node n1, Node n2)
{
    if(root == null)
        return -1;

    if(root.id == n1.id || root.id == n2.id)
        return root.id;

    int left = LCA2(root.left, n1, n2);
    int right = LCA2(root.right, n1, n2);

    if(left != -1 && right != -1)
        return root.id;

    if(left != -1)
        return left; // you don't need to call the same routine again here, which will cost you some time.

    return right; //Similar reason

}

答案 1 :(得分:1)

你已经得到了@Khaled和@Pham Trung的答案,但是让我帮你清除递归的呼叫追踪。

    1. LCA2(1, 4, 6) -> left = LCA2(2, 4, 6) = 2 (by 2 step because left = 4 != -1 and right = 6 != -1 means root.id is LCA which is 2)
                        right = LCA2(3, 4, 6) = -1 

    2. LCA2(2, 4, 6) -> left = LCA2(4, 4, 6) = 4 (by 2a step)
                        right = LCA2(5, 4, 6) = 6 (by 2b step)

    2a. LCA2(4, 4, 6) -> return root.id which is 4
    2b. LCA2(5, 4, 6) -> left = LCA2(6, 4, 6) = 6(by 2ba step)
                         right = LCA2(null, 4, 6) = -1  

    2ba. LCA2(6, 4, 6) -> return root.id which is 6

作为最终通话2!= -1所以它返回2作为答案 如果您需要更多说明,请恢复。 如果您遇到困难,可以在以下URL中找到geeksforgeeks的树问题的Java代码。 https://github.com/harmishlakhani/AlgoAndDataStructure/blob/master/AlgoAndDataStructure/src/com/ds/tree/BinaryTreeUtility.java