Question:
Convert A binary search tree to doubly linked list with In-order traversal.
Example:
Given A binary search tree:
4 / 2 5 / 1 3
Return1<->2<->3<->4<->5
Analysis:
There are many ways to ask this question:
1. A new non-circular doubly linked list is constructed from BST.
2. A new circular doubly linked list is constructed from BST.
3. Transform BST into a doubly linked list.
Issue 1: The middle sequence iterates through BST and constructs a new list node for each visit to a new tree node. Point the new list node's prev pointer to the previously visited list node, and then point to the new list node with the next pointer to the list node that was previously accessed. Because to record the information of the previously accessed list node, we create a state object that State.prev records the head node of the list Node,state.head record linked list before accessing it. How to judge the head node is simple, if State.prev equals null, indicating that there are no nodes before the current node, then the node is the head node. Helper function definition: A doubly linked list is constructed with a root-rooted BST, which records information about the head node and the last accessed node.
Issue 2: On the basis of question 1, add a new node to the list, point the Prev pointer of the head node to the new node, and then the new node's next pointer points to the head node. When the list is constructed, the entire list is naturally a circular chain list.
Question 3: Almost exactly the same as the previous question, because tree node has two pointers (left,right), and list node has two pointers (Prev,next). When you traverse BST, each time you access a tree node, its left pointer points to the tree node that was previously accessed, pointing to the current node the right pointer of the tree node that was previously accessed. Note: You cannot modify the right pointer of the tree node that is currently accessed because we need to find the next access tree node through the right pointer.
Code:
Non-cycle:
1 Public classSolution {2 /**3 * @paramroot:the root of tree4 * @return: The head of doubly list node5 */6 PublicDoublylistnode bsttodoublylist (TreeNode root) {7 if(Root = =NULL) {8 return NULL;9 }Ten One //Doublylistnode prev = null, head = NULL; Astate State =NewState (); - Helper (root, state); - the returnState.head; - } - - Private voidHelper (TreeNode root, State state) { + if(Root = =NULL) { - return; + } A at Helper (root.left, state); - -Doublylistnode node =NewDoublylistnode (root.val); -Node.prev =State.prev; - if(State.prev! =NULL) { -State.prev.next =node; in}Else { -State.head =node; to } + -State.prev =node; the Helper (root.right, state); * } $}
Cycle
1 /**2 * Definition of TreeNode:3 * public class TreeNode {4 * public int val;5 * Public TreeNode left and right;6 * Public TreeNode (int val) {7 * This.val = val;8 * This.left = This.right = null;9 * }Ten * } One * Definition for Doubly-listnode. A * public class Doublylistnode { - * int val; - * Doublylistnode Next, prev; the * Doublylistnode (int val) { - * This.val = val; - * This.next = This.prev = null; - * } + * } - */ + A //used to record the state of the list, Prev is the first node visited by Node,head at classState { - Doublylistnode prev; - Doublylistnode head; - - State (Doublylistnode prev, Doublylistnode head) { - This. prev =prev; in This. Head =head; - } to + State () { - This. prev =NULL; the This. Head =NULL; * } $ }Panax Notoginseng - //each time you create a new node, point it to the previous node, and the previous node points to it the Public classSolution { + /** A * @paramroot:the root of tree the * @return: The head of doubly list node + */ - PublicDoublylistnode bsttodoublylist (TreeNode root) { $ if(Root = =NULL) { $ return NULL; - } - the //Doublylistnode prev = null, head = NULL; -state State =NewState ();Wuyi Helper (root, state); the - returnState.head; Wu } - About Private voidHelper (TreeNode root, State state) { $ if(Root = =NULL) { - return; - } - A Helper (root.left, state); + theDoublylistnode node =NewDoublylistnode (root.val); -Node.prev =State.prev; $ if(State.prev! =NULL) { theState.prev.next =node; the}Else { theState.head =node; the } - in state.head.prev = node; the node.next = state.head; theState.prev =node; About Helper (root.right, state); the } the}
Complexity:
The time complexity is O (n), and N is the number of nodes in the tree.
Reference:
Https://github.com/JoyceeLee/Data_Structure/blob/master/Binary-Tree/Convert%20Binary%20Search%20Tree%20 (BST)% 20to%20sorted%20doubly-linked%20list.java
Convert Binary Search Tree to doubly Linked List