Enter a binary search tree to convert the tree to its image

Source: Internet
Author: User

Question 15th:
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.
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:
Struct BSTreeNode // a node in the binary search tree (BST)
{
Int m_nValue; // value of node
BSTreeNode * m_pLeft; // left child of node
BSTreeNode * m_pRight; // right child of node
};

[Cpp] # include <iostream>
# Include <iomanip>
# Include <stack>
Using namespace std;
// Defines the node of the Binary Search Tree:
Struct BSTreeNode // a node in the binary search tree (BST)
{
Int m_nValue; // value of node
BSTreeNode * m_pLeft; // left child of node
BSTreeNode * m_pRight; // right child of node
};
Void cfun (BSTreeNode * pt)
{
If (! Pt ){
Return;
}
BSTreeNode * ptem = pt-> m_pLeft;
Pt-> m_pLeft = pt-> m_pRight;
Pt-> m_pRight = ptem;
If (pt-> m_pLeft ){
Cfun (pt-> m_pLeft );
}
If (pt-> m_pRight ){
Cfun (pt-> m_pRight );
}
}
// Consider implementing this algorithm cyclically
Void MirrorIteratively (BSTreeNode * pTreeHead)
{
If (! PTreeHead)
Return;

Std: stack <BSTreeNode *> stackTreeNode;
StackTreeNode. push (pTreeHead );

While (stackTreeNode. size ())
{
BSTreeNode * pNode = stackTreeNode. top ();
StackTreeNode. pop ();

// Swap the right and left child sub-tree
BSTreeNode * pTemp = pNode-> m_pLeft;
PNode-> m_pLeft = pNode-> m_pRight;
PNode-> m_pRight = pTemp;

// Push left child sub-tree into stack if not null
If (pNode-> m_pLeft)
StackTreeNode. push (pNode-> m_pLeft );

// Push right child sub-tree into stack if not null
If (pNode-> m_pRight)
StackTreeNode. push (pNode-> m_pRight );
}
}
// This topic shows the method of recursion into a loop
Int main ()
{
 
System ("pause ");
Return 0;
}

# Include <iostream>
# Include <iomanip>
# Include <stack>
Using namespace std;
// Defines the node of the Binary Search Tree:
Struct BSTreeNode // a node in the binary search tree (BST)
{
Int m_nValue; // value of node
BSTreeNode * m_pLeft; // left child of node
BSTreeNode * m_pRight; // right child of node
};
Void cfun (BSTreeNode * pt)
{
If (! Pt ){
Return;
}
BSTreeNode * ptem = pt-> m_pLeft;
Pt-> m_pLeft = pt-> m_pRight;
Pt-> m_pRight = ptem;
If (pt-> m_pLeft ){
Cfun (pt-> m_pLeft );
}
If (pt-> m_pRight ){
Cfun (pt-> m_pRight );
}
}
// Consider implementing this algorithm cyclically
Void MirrorIteratively (BSTreeNode * pTreeHead)
{
If (! PTreeHead)
Return;
 
Std: stack <BSTreeNode *> stackTreeNode;
StackTreeNode. push (pTreeHead );
 
While (stackTreeNode. size ())
{
BSTreeNode * pNode = stackTreeNode. top ();
StackTreeNode. pop ();
 
// Swap the right and left child sub-tree
BSTreeNode * pTemp = pNode-> m_pLeft;
PNode-> m_pLeft = pNode-> m_pRight;
PNode-> m_pRight = pTemp;
 
// Push left child sub-tree into stack if not null
If (pNode-> m_pLeft)
StackTreeNode. push (pNode-> m_pLeft );
 
// Push right child sub-tree into stack if not null
If (pNode-> m_pRight)
StackTreeNode. push (pNode-> m_pRight );
}
}
// This topic shows the method of recursion into a loop
Int main ()
{

System ("pause ");
Return 0;
}


 

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.