Solution notes-enter a binary search tree to convert the tree to its image

Source: Internet
Author: User

Problem description: 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. Use recursive and cyclic methods to convert tree images.


For example, enter:

 

8


//


6 10


////


5 7 9 11


Output:


8


//


10 6


////


11 9 7 5


The nodes defining the binary search tree are:


View plaincopy to clipboardprint?
Struct BSTreeNode
{
Int value;
BSTreeNode * left;
BSTreeNode * right;
};
Struct BSTreeNode
{
Int value;
BSTreeNode * left;
BSTreeNode * right;
};
Idea: The question requires two methods: recursion and loop. The essence is the same.

Solution 1: use recursion. Assume that the current node is pNode. You only need to swap the left and right children of the node and then recursively solve the left and right subtree respectively. The code is extremely simple.

Solution 2: Use a loop. A secondary stack is required. Each time the top element of the stack is used to exchange the left and right children, the left and right children are pushed into the secondary stack respectively. When the elements in the stack are empty, end the loop. In fact, both recursion and loop are completed by using the stack features.

Reference code:


View plaincopy to clipboardprint?
// Function: enter a binary search tree to convert the tree to its image.
// Function parameter: pRoot is the root node.
// Return value: Root Node
BSTreeNode * Mirror_Solution1 (BSTreeNode * pRoot)
{
If (pRoot! = NULL)
{
BSTreeNode * pRight = pRoot-> right;
BSTreeNode * pLeft = pRoot-> left;
PRoot-> left = pai_solution1 (pRight); // convert the right subtree
PRoot-> right = pai_solution1 (pLeft); // convert the left subtree
}
Return pRoot;
}
// Function: enter a binary search tree to convert the tree to its image.
// Function parameter: pRoot is the root node.
// Return value: Root Node
BSTreeNode * Mirror_Solution1 (BSTreeNode * pRoot)
{
If (pRoot! = NULL)
{
BSTreeNode * pRight = pRoot-> right;
BSTreeNode * pLeft = pRoot-> left;
PRoot-> left = pai_solution1 (pRight); // convert the right subtree
PRoot-> right = pai_solution1 (pLeft); // convert the left subtree
}
Return pRoot;
}

View plaincopy to clipboardprint?
BSTreeNode * Mirror_Solution2 (BSTreeNode * pRoot)
{
If (pRoot! = NULL)
{
Stack <BSTreeNode *> stk; // auxiliary stack
Stk. push (pRoot); // press it into the root node
While (stk. size ())
{
BSTreeNode * pNode = stk. top ();
BSTreeNode * pLeft = pNode-> left;
BSTreeNode * pRight = pNode-> right;
Stk. pop ();
 
If (pLeft! = NULL)
Stk. push (pLeft );
If (pRight! = NULL)
Stk. push (pRight );
PNode-> left = pRight; // exchange left and right children
PNode-> right = pLeft;
}
}
Return pRoot;
}

This article is from the "wuzw.ai column"

Related Article

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.