Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/elixir/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java中递归的最低公共祖先_Java - Fatal编程技术网

Java中递归的最低公共祖先

Java中递归的最低公共祖先,java,Java,我在leetcode中找到了java中最低共同祖先问题的解决方案。另一个问题是,找到p和q的最低共同祖先,BST根在根上。 这是我的密码 public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) { if(root == null || root == p || root == q) return root; TreeNode left = lowestCommonAnc

我在leetcode中找到了java中最低共同祖先问题的解决方案。另一个问题是,找到p和q的最低共同祖先,BST根在根上。 这是我的密码

public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        if(root == null || root == p || root == q) return root;
        TreeNode left = lowestCommonAncestor(root.left, p, q);
        TreeNode right = lowestCommonAncestor(root.right, p, q);
        return left != null && right != null ? root : left == null?right :left;

    }
虽然这适用于大多数情况,但如果树是这样的,并且问题是最低共同祖先(1,2,3)或最低共同祖先2和3,其中root==1

1 -> 2 -> 3
在我看来,这个解决方案提供的答案是
2
, 这是因为在递归之后

left = null
right = 2
而实际答案是1

然而,这个解决方案是有效的。有人能帮我理解我在这里犯了什么错误。

遵循逻辑:

lowestCommonAncestor(root=1, p=2, q=3):
    if (root == null || root == p || root == q) return root;
    //     false           false        false

    left = lowestCommonAncestor(2, 2, 3):
               if (root == null || root == p || root == q) return root
               //     false            true                return 2

    right = lowestCommonAncestor(null, 2, 3):
                if (root == null || root == p || root == q) return root;
                //      true                                return null

    return left != null && right != null ? root : left == null ? right : left;
    //         true        false                     false             : 2
结果:
2

遵循代码的最简单方法是使用调试器遵循逻辑:

lowestCommonAncestor(root=1, p=2, q=3):
    if (root == null || root == p || root == q) return root;
    //     false           false        false

    left = lowestCommonAncestor(2, 2, 3):
               if (root == null || root == p || root == q) return root
               //     false            true                return 2

    right = lowestCommonAncestor(null, 2, 3):
                if (root == null || root == p || root == q) return root;
                //      true                                return null

    return left != null && right != null ? root : left == null ? right : left;
    //         true        false                     false             : 2
结果:
2


遵循代码的最简单方法是在执行
TreeNode right=lowerstcommon祖先(root.right,p,q)之后使用调试器,

你会得到:

left=null;
right=2;
最后,结果=
(left!=null&&right!=null)?root:(left==null?right:left)


返回结果:
2

执行
TreeNode right=lowerstcommon祖先(root.right,p,q),

你会得到:

left=null;
right=2;
最后,结果=
(left!=null&&right!=null)?root:(left==null?right:left)


返回结果:
2

在这个场景中
p
q
是什么?不,我的意思是在后一部分,您提供了树结构,但没有为方法提供其他两个输入参数,因此很难讨论发生了什么。在这个场景中
p
q
是什么?不,我的意思是在后一部分,您已经为方法提供了树结构,但没有提供其他两个输入参数,因此很难讨论发生了什么。