Sword refers to Offer interview question 27 (Java edition): Binary Search Tree and two-way linked list, sword refers to offer

Source: Internet
Author: User

Sword refers to Offer interview question 27 (Java edition): Binary Search Tree and two-way linked list, sword refers to offer

Question: enter a binary search tree and convert it into a sorted two-way linked list. It is required that a new node cannot be created. You can only adjust the point of the node pointer in the tree.

For example, in the binary search tree, the two-way linked list after the output conversion is:


In a binary tree, each node has two pointers to sub-nodes. In a two-way linked list, each node also has two pointers, respectively pointing to the forward node and the next node. Because the structures of these two nodes are similar, and the binary search tree is also a Sort data structure, it is theoretically possible to implement the conversion of the Binary Search Tree and the sorted two-way linked list. In a binary tree, the value of the Left subnode is always smaller than that of the parent node, and the value of the right subnode is always greater than that of the parent node. Therefore, when we convert the two-way linked list called sorting, the pointer to the left subnode is adjusted to the pointer of the forward node in the linked list, the pointer to the right subnode is changed to the pointer to the next node in the two tables. Next we will consider how to convert.

Since the converted linked list is required to be sorted, we suspect that the central order traverses every node in the tree, because the central order traversal algorithm is characteristic of traversing every node of the Binary Tree in ascending order. When traversing to the root node, we divide the tree into three parts: a node with a value of 10, a left subtree with a root node of 6, and a right subtree with a root node of 14. According to the definition of the sort linked list, the knot with a value of 10 is linked to the largest node of its left subtree (that is, the node with a value of 8, at the same time, it will link to the smallest node of the right subtree (that is, the node with the value of 12,


In the order of sequential traversal, when we traverse and convert to the root node (with a value of 10), its left subtree has been converted into a sorted linked list, and the last node in the linked list is the largest node of the current value. We link the root node with the value of 8, and the last node in the linked list is 10. Next, we traverse and convert the right subtree and link the root node with the smallest node of the right subtree. As for how to convert its left and right subtree, since the traversal and conversion process are the same, we naturally think of recursion.

Java code implementation:

/*** Enter a binary search tree and convert it into a sorted two-way linked list. * You cannot create any new node. You can only adjust the point of the node pointer in the tree. */Package swordForOffer; import utils. binaryTreeNode;/*** @ author JInShuangQi *** February */public class E27ConvertBinarySearchTree {public BinaryTreeNode convert (BinaryTreeNode root) {BinaryTreeNode node = null; convert (root, node ); while (node! = Null & node. leftNode! = Null) {node = node. leftNode;} return node;} public void convert (BinaryTreeNode root, BinaryTreeNode lastNode) {if (root = null) return; BinaryTreeNode current = root; if (current. leftNode! = Null) convert (current. leftNode, lastNode); current. leftNode = lastNode; if (lastNode! = Null) lastNode. rightNode = current; if (current. rightNode! = Null) convert (current. rightNode, lastNode );}}


Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.