[Sword Point offer] binary search tree and two-way linked list

Source: Internet
Author: User

Title Description

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.

Enter a description

Binary search Tree

Output description

Sort of doubly linked list

Problem analysis

Node Description:

publicclass TreeNode {    int0;    null;    null;    publicTreeNode(int val) {        this.val = val;    }}

Binary lookup tree (English: Binary search tree), also known as the binary searching trees, ordered binary tree (English: Ordered binary tree), sort binary (English: sorted binary trees), refers to an empty tree or a two-fork tree with the following properties:

    • The left subtree of any node is not empty, then the value of all nodes on the left subtree is less than the value of its root node;

    • The right subtree of any node is not empty, then the value of all nodes on the right subtree is greater than the value of its root node;

    • The left and right subtrees of any node are also two-fork search tree;

    • There are no nodes with key values equal.

Solution One (Recursive) run time: 32ms Memory: 550k

//Recursive call Zogen right traversal Public classSolution {//The left and right head nodes of the doubly linked listTreeNode Lefthead =NULL; TreeNode Righthead =NULL; PublicTreeNodeConvert(TreeNode Prootoftree) {//Recursive call the left and right nodes of the leaf node return null        if(prootoftree==NULL)return NULL;//First run, it makes the leftmost leaf node the first node in the listConvert (Prootoftree.left);if(righthead==NULL) {lefthead= righthead = Prootoftree; }Else{//Righthead The root node to the right of the doubly linked list, moving backwardsRighthead.right = Prootoftree;           Prootoftree.left = Righthead;        Righthead = Prootoftree; }//The Right leaf node is also inserted into the doubly linked list (Righthead has been determined, directly inserted)Convert (Prootoftree.right);//Return to the left head node.        returnLefthead; }}

Find the leftmost leaf node of the tree, take the node as the first node of the doubly linked list, then insert the node to the right of the doubly linked list and move the Righthead pointer, and finally return Lefthead, according to the Zogen right traversal.

Solution two run time: 31ms occupied memory: 503k

Import Java.util.Stack; Public classSolution { PublicTreeNodeConvert(TreeNode Prootoftree) {if(Prootoftree = =NULL)returnProotoftree;//The left and right head nodes of the doubly linked listTreeNode Lefthead =NULL; TreeNode Righthead =NULL; stack<treenode> s =NewStack<treenode> (); while(Prootoftree! =NULL|| !s.isempty ()) {if(Prootoftree! =NULL) {//traverse to the leftmost leaf node in turnS.push (Prootoftree);            Prootoftree = Prootoftree.left; }Else{prootoftree = S.pop ();if(Righthead = =NULL)//With the leftmost leaf node as the linked list headerLefthead = Righthead = Prootoftree;Else{righthead.right = Prootoftree;                    Prootoftree.left = Righthead;                Righthead = Prootoftree;            } prootoftree = Prootoftree.right; }        }returnLefthead; }}

In the same way, the leftmost leaf node is the linked list head, and the stack traversal list is borrowed.

[Sword Point offer] binary search tree and doubly linked list

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.