Java implements binary search tree and doubly linked list

Input a binary search tree, convert the binary search tree into a sorted doubly linked list. It is required that no new nodes can be created, and only the point of the node pointer in the tree can be adjusted.

code

    static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        public TreeNode(int val) {
            this.val = val;
        }
    }

    public static TreeNode convertTree2LinkedList(TreeNode root) {
        if (root == null) {
            return null;
        }
        TreeNode head = convert(root);
        // Find the head node of the linked list, that is, the left pointing to the previous node is null
        while (head.left != null) {
            head = head.left;
        }
        return head;
    }

    /**
     * using recursion
     * In a doubly linked list, the left node points to the previous node, and the right points to the next node
     * @param root
     * @return
     */
    private static TreeNode convert(TreeNode root) {
        if (root == null) {
            return null;
        }
        // If there is a left subtree, convert the left subtree to a doubly linked list
        if (root.left != null) {
            // left subtree doubly linked list
            TreeNode left = convertTree2LinkedList(root.left);
            // Because right points to the next node in the doubly linked list, find the node whose right child node is null as the tail node of the left subtree doubly linked list
            while (left.right != null) {
                left = left.right;
            }
            // Put the root node of the tree after the tail node of the left subtree doubly linked list
            root.left = left;
            left.right = root;
        }
        // If there is a right subtree, convert the right subtree to a doubly linked list
        if (root.right != null) {
            // right subtree doubly linked list
            TreeNode right = convertTree2LinkedList(root.right);
            // Because left points to the previous node in the doubly linked list, find the node whose left child node is null as the head node of the right subtree doubly linked list
            while (right.left != null) {
                right = right.left;
            }
            // Put the head node of the right subtree doubly linked list after the root node of the tree
            root.right = right;
            right.left = root;
        }
        // Recursive termination condition, return if there is no node with left or right child nodes
        return root;
    }

    public static void main(String[] args) {
        TreeNode root = buildTree();
        root = convertTree2LinkedList(root);
        // output 2 3 4 5 7
        while (root != null) {
            System.out.print(root.val + " ");
            root = root.right;
        }
    }

    /**
     * Create tree:
     *              5
     *          3       7
     *      2       4
     * @return
     */
    private static TreeNode buildTree(){
        TreeNode root = new TreeNode(5);
        TreeNode left = new TreeNode(3);
        TreeNode left1 = new TreeNode(2);
        TreeNode right1 = new TreeNode(4);
        left.left = left1;
        left.right = right1;
        TreeNode right = new TreeNode(7);
        root.left = left;
        root.right = right;
        return root;
    }

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325925463&siteId=291194637