Enter a binary search tree and convert the two-fork search tree into a sorted doubly linked list. Requires that no new nodes can be created, only the point pointer of the node in the tree can be adjusted.
It's been a long time, but it's not as hard to imagine as it seems.
Like a tree.
In fact, the order of the list is the root of this tree in the order of traversal, so I think of recursion or non-recursive.
The non-recursive approach is to use the stack to traverse the tree in the root,
The idea of using the stack for root traversal is that the root node's Zuozi first into the stack, then out of the stack, the stack top element of the right child into the stack until the empty stack.
When building a doubly-linked list, record the previous node in the stack previous, current out-of-stack nodes, and then associate them.
public static TreeNode Convert (TreeNode prootoftree) {if (prootoftree==null) {return null;} if (prootoftree.left==null& Amp;&prootoftree.right==null) {return prootoftree;}//Create a stack linkedlist stack = new LinkedList (); TreeNode node =prootoftree; All left nodes into the stack while (node!=null) {Stack.push (node); node=node.left;} TreeNode prehead=new TreeNode (0); TreeNode Previous=prehead; When the stack is not empty while (!stack.isempty ()) {//pops the top element of the stack TreeNode current= (TreeNode) Stack.pop ();//stack top element right child into the stack if (current.right!= NULL) {Stack.push (current.right); node=current.right.left; while (node!=null) {Stack.push (node); node=node.left;}}// The point before the stack and now the point of the stack to establish contact previous.right=current; current.left=previous; previous=current; } TreeNode Newhead=prehead.right; Newhead.left=null; return newhead; }
Recursive practice I think for a long time, because I always think of how to put down the previous traversal, the sword refers to the inside of an offer is not right.
Ideas:
In fact, the left side of the root node is the last node that points to the Zuozi, the rightmost node, and the right of the root node is the leftmost node of the rightmost subtree.
According to this rule, it can be implemented recursively.
public static TreeNode convert (TreeNode prootoftree) {if (prootoftree==null) {return null;} if (prootoftree.left==null&&prootoftree.right==null) {return prootoftree;} The left subtree is constructed into a doubly linked list and returned to the head node TreeNode Left=convert (Prootoftree.left); TreeNode p=left;//will Zuozi the last node to get while (P!=null&&p.right!=null) {p=p.right;} if (p!=null) {prootoftree.left=p;p.right=prootoftree;} Constructs the right subtree into a doubly linked list and returns the head node TreeNode Right=convert (prootoftree.right);p =right;//Gets the first node of the right subtree while (p!=null&&p.left !=null) {p=p.left;} If p is not empty, associate Prootoftreeif (p!=null) {prootoftree.right=p;p.left=prootoftree;} Determines if left is empty to avoid the absence of a subtree return left==null?prootoftree:left;}
offer-Two forks search tree and doubly linked list