Notes for solving Microsoft interview questions-image conversion of Binary Search Tree

Source: Internet
Author: User

(The question comes fromV_july_vSorting, Microsoft and other data structures + algorithm interview 100 questions, July blogHttp://blog.csdn.net/v_JULY_v)

Question: enter a binary search tree to convert the tree to its image,
That is, in the converted Binary Search Tree, the left subtree has more nodes than the right subtree.
UseRecursionAndLoopTwo methods are used to convert the image of the tree.
For example, enter:
8
/\
6 10
/\/\
5 7 9 11

Output:
8
/\
10 6
/\/\
11 9 7 5

Solution: In this binary search tree, to convert images, you only need to modify the orientation of the left and right nodes. First, you need to think of the first, middle, and subsequent steps when using recursive calls, if you use the first or subsequent traversal implementation, the difference is that the first order is to start modifying the root node, and the second is to start modifying the next traversal from the leaf node, the middle-order traversal process is a small to large traversal process in the binary search tree. Although it can be implemented, additional temporary variables are required to save the pointer value. Here we use post-order traversal.

Struct bstreenode {int m_nvalue; // value of nodebstreenode * m_pleft; // left child of nodebstreenode * m_pright; // right child of node}; typedef bstreenode * tree; void addnode (Tree & tree, int data) {If (tree = NULL) {tree = new bstreenode; tree-> m_nvalue = data; tree-> m_pleft = NULL; tree-> m_pright = NULL; return;} else if (tree-> m_nvalue> = data) {addnode (tree-> m_pleft, data);} elseaddnode (tree-> m_pright, Data);} void translate (Tree & tree) {If (! Tree) {return;} If (tree-> m_pleft) {translate (tree-> m_pleft);} If (tree-> m_pright) {translate (tree-> m_pright );} tree temp = tree-> m_pleft; tree-> m_pleft = tree-> m_pright; tree-> m_pright = temp;} void printtree (Tree & tree) {If (! Tree) {return;} printtree (tree-> m_pleft); cout <tree-> m_nvalue <''; printtree (tree-> m_pright );} int _ tmain (INT argc, _ tchar * argv []) {tree binarytree = NULL; // header node // bool bend = false; // while (! Bend) // {// int DATA = 0; // CIN> data; // If (Data =-1) // {// bend = true; //} // else // addnode (binarytree, data); //} addnode (binarytree, 8); addnode (binarytree, 6); addnode (binarytree, 10 ); addnode (binarytree, 5); addnode (binarytree, 7); addnode (binarytree, 9); addnode (binarytree, 11); translate (binarytree); printtree (binarytree ); system ("pause"); Return 0 ;}

Idea: if we adopt a circular method, we use containers to save all nodes in the container and traverse them cyclically.

Int _ tmain (INT argc, _ tchar * argv []) {tree binarytree = NULL; // header node // bool bend = false; // while (! Bend) // {// int DATA = 0; // CIN> data; // If (Data =-1) // {// bend = true; //} // else // addnode (binarytree, data); //} addnode (binarytree, 8); addnode (binarytree, 6); addnode (binarytree, 10 ); addnode (binarytree, 5); addnode (binarytree, 7); addnode (binarytree, 9); addnode (binarytree, 11); // translate (binarytree ); vector <tree> nodevct; nodevct. push_back (binarytree); int start = 0; int end = 1; TREE tree = NULL; tree temp = NULL; while (start <End) {tree = nodevct [start]; if (tree) {temp = tree-> m_pleft; tree-> m_pleft = tree-> m_pright; tree-> m_pright = temp;} If (tree-> m_pleft) {nodevct. push_back (tree-> m_pleft); ++ end;} If (tree-> m_pright) {nodevct. push_back (tree-> m_pright); ++ end ;}++ start ;}printtree (binarytree); System ("pause"); Return 0 ;}

Answers provided by July

void Revertsetree(list *root){    if(!root)       return;    list *p;    p=root->leftch;    root->leftch=root->rightch;    root->rightch=p;    if(root->leftch)      Revertsetree(root->leftch);    if(root->rightch)      Revertsetree(root->rightch);}

Method 2

Because the essence of recursion is that the compiler generates a function call stack,
Therefore, the simplest way to use a loop to complete the same task is to use an auxiliary stack to simulate recursion.

First, we put the header node of the tree into the stack.
In a loop, as long as the stack is not empty, the top node of the stack pops up and switches its left and right subtree.

If it has a left subtree, press its left subtree into the stack;
If it has a right subtree, press its right subtree into the stack.

In this way, the left and right subtree of its son node can be exchanged in the next loop.

// Use the auxiliary stack to simulate recursion and change it to a loop (if there is an error, expect it to be correct): void revertsetree (list * phead) {If (! Phead) return; stack <list *> stacklist; stacklist. Push (phead); // first place the header node of the tree into the stack. While (stacklist. size () // in a loop, as long as the stack is not empty, the top node of the stack pops up, exchanging its left and right subtree {list * pnode = stacklist. top (); stacklist. pop (); list * ptemp; ptemp = pnode-> leftch; pnode-> leftch = pnode-> rightch; pnode-> rightch = ptemp; If (pnode-> leftch) stacklist. push (pnode-> leftch); // if there is a left subtree, press its left subtree into the stack if (pnode-> rightch) stacklist. push (pnode-> rightch); // if there is a right subtree, press its right subtree into the stack }}

What I think after reading the answer:

① When recursive methods are used to solve the problem, the function parameters are always involved in the stack entry and exit. We can reasonably choose the auxiliary stack to solve this problem.

② Sequential traversal and post-sequential traversal have no effect on the results.

③ When the secondary stack is used, the traversal order is actually the right left of the root.

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.