有人可以告诉我我错了吗?

时间:2020-08-02 07:44:23

标签: tree

findrightsiblings获取一个节点引用并返回其右同级,即,其右侧的最左侧节点。它抛出一个空指针异常。请帮忙。

public static Node find(Node root, int k) {
    if (root == null)
        return null;

    if (k == 0)
        return root;

    Node m = find(root.left, k - 1);

    if (m == null)
        m = find(root.right, k - 1);

    return m;
}

public static Node findRightSibling(Node node) {
    Node y = node.parent;
    int count = 1;

    while (true) {
        if (y.right != null) {
            Node js = find(y.right, count - 1);

            if (js != null)
                return js;
        }

        y = y.parent;
        count++;
    }
}

0 个答案:

没有答案