当前位置: 首页 > 知识库问答 >
问题:

查找树搜索中的最低命令祖先失败(不是算法问题,而是Java问题)

包沈义
2023-03-14

非常简单的树问题,结果是null(应该是1,因为1是2&3的父节点),找不到原因。该方法本身已通过Leetcode在线判断。

以下是问题的链接:

public class LowestCommonAncestor2 {

    public static TreeNode mockTree() {
        TreeNode node1 = new TreeNode(1);
        TreeNode node2 = new TreeNode(2);
        TreeNode node3 = new TreeNode(3);
        node1.left = node2;
        node1.right = node3;
        return node1;
    }

    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode t1, TreeNode t2) {
           if (root == null || root == t1 || root == t2) {
               return root;
           }

           TreeNode left = lowestCommonAncestor(root.left, t1, t2);
           TreeNode right = lowestCommonAncestor(root.right, t1, t2);

           if (left != null && right != null) 
               return root;
           if (left != null) 
               return left;
           if (right != null) 
               return right;
           return null;
    }

    public static void main (String [] args) {
        LowestCommonAncestor2 test = new LowestCommonAncestor2();
        TreeNode root = mockTree();
        TreeNode target1 = new TreeNode(2);
        TreeNode target2 = new TreeNode(3);
        TreeNode result = test.lowestCommonAncestor(root, target1, target2);
        System.out.println(result);
    } 
}

和TreeNode的定义如下:

public class TreeNode {
     public int val;
     public TreeNode left, right;
     public TreeNode(int val) {
         this.val = val;
         this.left = this.right = null;
     }
}

共有1个答案

诸葛皓
2023-03-14

发生错误是因为这行代码

if (root == null || root == t1 || root == t2) 

在执行此方法时,请仔细考虑您要比较的是什么

TreeNode result = test.lowestCommonAncestor(root, target1, target2);

当您调用LCA方法时,第一个if语句中的任何条件都不会计算为true,因为没有一个语句是true,这是正确的。但是,现在让我们来讨论第一个递归调用,

TreeNode left = lowestCommonAncestor(root.left, t1, t2);
TreeNode right = lowestCommonAncestor(root.right, t1, t2);

在TreeNode类中创建一个equals方法,该方法返回一个布尔值并接受另一个TreeNode对象作为参数。此外,请确保此方法是实例方法而不是静态方法,以便在代码中执行此方法时如下所示,

public boolean equals(TreeNode node){ //method address
    //rest of the method has to be implemented yourself
    //ask yourself, how are two treenodes considered equal in your situation?
}

最后,将root==t1替换为

root.equals(t1)

根==t2

root.equals(t2)
 类似资料:
  • 如果我拆分while循环,有什么不同吗?谢了!

  • 我正试图解决这个问题,但我遇到了一些麻烦: 在二进制搜索树(BST)中: 节点左子树中每个节点的数据值小于该节点的数据值。 节点右侧子树中每个节点的数据值大于该节点的数据值。 如您所见,节点(4)位于节点(3)的左侧子树中,尽管4大于3,因此方法应该返回。但是,我的代码返回。 我怎么能控制这个案子?如何检查左/右子树中的所有值都低于/大于根(不仅是直接子树)?

  • 我只用if语句解决了这个问题。它可以用其他各种方法来解决。我刚刚开始编程,所以我想不出使用任何其他数据结构来解决这个问题。 我的问题: 如何在各种方法中选择最好的方法?和我的直接天真方法相比,这种最佳方法的优点/缺点是什么。 不仅针对这个问题,总的来说。如何解决任何问题? 我的代码: 问题链接:https://www.hackerrank.com/challenges/binary-search-

  • 我的问题是:“这是已知算法的已知问题吗?”。我怀疑是。它似乎非常类似于拓扑排序。我有一个基于合并排序的算法的想法,但如果已知的算法已经存在,就没有理由提出我自己的算法。

  • 问题内容: 这是一个受欢迎的面试问题,我唯一可以找到的有关该主题的文章是TopCoder的文章。对我来说不幸的是,从访谈答案的角度来看,它看起来过于复杂。 除了绘制到两个节点的路径并推导祖先之外,没有其他更简单的方法了吗?(这是一个很流行的答案,但是面试题有一个变体,要求一个固定的空格答案)。 问题答案: 恒定空间答案:(尽管不一定有效)。 有一个函数findItemInPath(int inde

  • 我的问题是树中有大量的节点和许多查询。是否有一种算法,它进行预处理,使查询能够在恒定的时间内得到答复。 我研究了使用RMQ的LCA,但我不能使用该技术,因为我不能对树中的这么多节点使用数组。 如果知道它是满二叉树,节点之间的关系如上所示,那么有人能给我一个高效的实现来快速回答许多查询。 但是当有很多查询时,这种算法非常耗时,因为在最坏的情况下,我可能必须遍历30的高度(树的最大高度)才能到达根(最