Microsoft Interview 100 Questions Series algorithm experience

Source: Internet
Author: User

Microsoft 100 Questions Series address

Answer address

The essay, when it is his own practice in this kind of algorithm some ideas, some experience, some understanding, some reference, when self-citation, will be attached to the corresponding link!

Title: Turn the two-dollar lookup tree into a sorted doubly linked list (tree)

Description: Enter a two-dollar lookup tree to convert the two-dollar lookup tree into a sorted doubly linked list. Requires that no new nodes be created, only the pointer is adjusted.

Thinking process [personal thinking]:

1. The two-dollar lookup tree refers to any node in which the value on its left subtree is less than the value of the current node, and its right subtree is greater than the value of the current node, and for equal values it depends on the principle of Zuozi or right subtree.

2. The algorithm of the tree must be able to think of some keywords: traversal algorithm [pre-sequence traversal, sequence traversal, post-order traversal, hierarchical traversal], the algorithm complexity O (LOGN), O (1), O (N), O (Nlogn), most often think of is O (logn). Recursive algorithm and non-recursive algorithm [ Recursive algorithms are most widely used in trees], the characteristics and advantages of various types of trees [common tree has bst,avl,b+-, heap, stretch tree, line tree, etc.], tree implementation [static implementation, pointer implementation], tree node information [saving the corresponding information is sometimes very important]

3. Look at the original question example, we can see that the core of the problem is: the middle sequence traversal, with descriptive thinking, Zo Shu first, then is himself, and then the right tree. Middle Sequence traversal common recursive implementation, non-recursive more cumbersome

4. Contact the requirements, to be ranked in a doubly linked list, then the first thought is the middle sequence to traverse all the nodes, return to the sequence of sequential traversal, and then connected to a doubly linked list, need to pay attention to the place is: head node without the order of the node, the end of the node has no post-orders node.

The following is the corresponding code [wrong please testify, thank you!]

Definition of BST Tree:

1 struct node{2     Node * Left ; 3     Node * Right ; 4     int value; 5     Node (): Left (null), right (null) {}6 };

Middle Sequence Traversal:

1Vector<node*> convert_doubledlinked (Node *root)2 {3     if(Root = NULL)returnVector<node*>();4Auto L = convert_doubledlinked (root->Left );5Auto R = convert_doubledlinked (root->Right );6 L.push_back (root);7      for(ConstAuto &v:r)8 L.push_back (v);9 Ten     returnl; One}
View Code

Make a list and get the head node:

1Node * Get_head (vector<node*>node_vector) {2     if(Node_vector.empty ())returnNULL;3Node * head =NULL;4Node * prev =NULL;5 6      for(Auto &v:node_vector) {7         if(Head = =NULL)8Head =v;9 Ten         if(Head! =v) { OnePrev->right =v; AV->left =prev; -         } -  thePrev =v; -V->right =NULL; -     } -Head->left =NULL; +     returnhead; -}
View Code

5. Of course, we may want to do not split into two parts, want to merge it in a recursive, so first of all to understand is the current node in the doubly linked list of the first sequence node is which, the current node is the next order node is which, it is not difficult to think, the pre-order node is record the last traversal of the node, Descriptive: In the left subtree of the current node to the right, go to the end of that point is, the post-order node is the right sub-tree the first point of traversal, descriptive said: In the current node of the right sub-tree to the left, go to the end of the node. We have a number of ways to get these two values, such as by definition to find, for example, the tree connected to a doubly linked list, we know everything. Sometimes think about, just need the information we need, I hope Zuozi give me the pre-order node, right sub-tree to my post-sequence knot, thinking, I hope that the sub-tree recursion, it can give me the leftmost node and the rightmost node, so the recursive function appended two reference return value can, of course, I can also use each node to hold exactly two nodes, and combine the two parameters into one.

The code is as follows:

1Node * convert_doubledlinked (node * root, Node &data) {2     if(Root = =NULL) {3Data.left =NULL;4Data.right =NULL;5         returnRoot;6     }7 8 Node ta, TB;9 TenNode * L = convert_doubledlinked (root->Left , TA); OneNode * r = convert_doubledlinked (root->Right , TB); A  -     if(Ta.right! =NULL) { -Ta.right->right =Root; theRoot->left =Ta.right; -     } -     if(Tb.left! =NULL) { -Root->right =Tb.left; +Tb.left->left =Root; -     } +  AData.left = (Ta.left = = NULL)?Root:ta.left; atdata.right= (Tb.right = = NULL)?Root:tb.right; -  -     returnData.left; -}
View Code

Microsoft Interview 100 Questions Series algorithm experience

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.