美文网首页
在二叉树中找到一个节点的后继节点

在二叉树中找到一个节点的后继节点

作者: Ramsey16k | 来源:发表于2019-11-26 12:50 被阅读0次

    【题目】 现在有一种新的二叉树节点类型如下:
    public class Node {
    public int value;
    public Node left;
    public Node right;
    public Node parent;
    public Node(int data) { this.value = data; }
    }
    该结构比普通二叉树节点结构多了一个指向父节点的parent指针。假
    设有一 棵Node类型的节点组成的二叉树,树中每个节点的parent指针
    都正确地指向 自己的父节点,头节点的parent指向null。只给一个在
    二叉树中的某个节点 node,请实现返回node的后继节点的函数。在二
    叉树的中序遍历的序列中, node的下一个节点叫作node的后继节点。

    public static Node getSuccessorNode(Node node) {
            if (node == null) {
                return null;
            }
            if (node.right != null) { //当前节点存在右子树
                return getLeftMost(node.right); //找右子树最左的节点
            } else { // 没有右子树
                Node parent = node.parent; //找父节点
                while (parent != null && parent.left != node) {
                    //当前节点没有父节点 or 当前节点是其父节点的左孩子时,循环结束
                    node = parent;
                    parent = node.parent;
                }
                return parent;
            }
        }
    

    完整代码:

    public class SuccessorNode {
    
        public static class Node {
            public int value;
            public Node left;
            public Node right;
            public Node parent;
    
            public Node(int data) {
                this.value = data;
            }
        }
    
        public static Node getSuccessorNode(Node node) {
            if (node == null) {
                return null;
            }
            if (node.right != null) { //当前节点存在右子树
                return getLeftMost(node.right); //找右子树最左的节点
            } else { // 没有右子树
                Node parent = node.parent; //找父节点
                while (parent != null && parent.left != node) {
                    node = parent;
                    parent = node.parent;
                }
                return parent;
            }
        }
    
        public static Node getLeftMost(Node node) {
            if (node == null) {
                return null;
            }
            while (node.left != null) {
                node = node.left;
            }
            return node;
        }
    
        public static void main(String[] args) {
            Node head = new Node(6);
            head.parent = null;
            head.left = new Node(3);
            head.left.parent = head;
            head.left.left = new Node(1);
            head.left.left.parent = head.left;
            head.left.left.right = new Node(2);
            head.left.left.right.parent = head.left.left;
            head.left.right = new Node(4);
            head.left.right.parent = head.left;
            head.left.right.right = new Node(5);
            head.left.right.right.parent = head.left.right;
            head.right = new Node(9);
            head.right.parent = head;
            head.right.left = new Node(8);
            head.right.left.parent = head.right;
            head.right.left.left = new Node(7);
            head.right.left.left.parent = head.right.left;
            head.right.right = new Node(10);
            head.right.right.parent = head.right;
    
            Node test = head.left.left;
            System.out.println(test.value + " next: " + getSuccessorNode(test).value);
            test = head.left.left.right;
            System.out.println(test.value + " next: " + getSuccessorNode(test).value);
            test = head.left;
            System.out.println(test.value + " next: " + getSuccessorNode(test).value);
            test = head.left.right;
            System.out.println(test.value + " next: " + getSuccessorNode(test).value);
            test = head.left.right.right;
            System.out.println(test.value + " next: " + getSuccessorNode(test).value);
            test = head;
            System.out.println(test.value + " next: " + getSuccessorNode(test).value);
            test = head.right.left.left;
            System.out.println(test.value + " next: " + getSuccessorNode(test).value);
            test = head.right.left;
            System.out.println(test.value + " next: " + getSuccessorNode(test).value);
            test = head.right;
            System.out.println(test.value + " next: " + getSuccessorNode(test).value);
            test = head.right.right; // 10's next is null
            System.out.println(test.value + " next: " + getSuccessorNode(test));
        }
    
    }
    

    相关文章

      网友评论

          本文标题:在二叉树中找到一个节点的后继节点

          本文链接:https://www.haomeiwen.com/subject/pglpwctx.html