题目:输入一颗二元查找树,将该树转换为它的镜像,即在转换后的二元查找树中,左子树的结点都大于右子树的结点。用递归和循环两种方法完成树的镜像转换。
例如输入:
8
/ \
6 10
/\ /\
5 7 9 11
输出:
8
10 6
/\ /\
11 9 7 5
定义二元查找树的结点为:
分析:尽管我们可能一下子不能理解镜像是什么意思,但上面的例子给我们的直观感觉,就是交换结点的左右子树。我们试着在遍历例子中的二元查找树的同时来交换每个结点的左右子树。遍历时首先访问头结点8,我们交换它的左右子树得到:
8
10 6
/\ /\
9 11 5 7
我们发现两个结点6和10的左右子树仍然是左结点的值小于右结点的值,我们再试着交换他们的左右子树,得到:
11 9 7 5
刚好就是要求的输出。
上面的分析印证了我们的直觉:在遍历二元查找树时每访问到一个结点,交换它的左右子树。这种思路用递归不难实现,将遍历二元查找树的代码稍作修改就可以了。参考代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<code>///////////////////////////////////////////////////////////////////////</code>
<code>// Mirror a BST (swap the left right child of each node) recursively</code>
<code>// the head of BST in initial call</code>
<code>void</code> <code>MirrorRecursively(BSTreeNode *pNode)</code>
<code>{</code>
<code> </code><code>if</code><code>(!pNode)</code>
<code> </code><code>return</code><code>;</code>
<code> </code><code>// swap the right and left child sub-tree</code>
<code> </code><code>BSTreeNode *pTemp = pNode->m_pLeft;</code>
<code> </code><code>pNode->m_pLeft = pNode->m_pRight;</code>
<code> </code><code>pNode->m_pRight = pTemp;</code>
<code> </code><code>// mirror left child sub-tree if not null</code>
<code> </code><code>if</code><code>(pNode->m_pLeft)</code>
<code> </code><code>MirrorRecursively(pNode->m_pLeft); </code>
<code> </code><code>// mirror right child sub-tree if not null</code>
<code> </code><code>if</code><code>(pNode->m_pRight)</code>
<code> </code><code>MirrorRecursively(pNode->m_pRight);</code>
<code>}</code>
得出求一棵树的镜像的过程:先前序遍历这棵树的每个结点,如果遍历到的结点有子结点,就交换它的两个子结点。当交换完所有非叶子结点的左右结点之后,就得到了树的镜像。
由于递归的本质是编译器生成了一个函数调用的栈,因此用循环来完成同样任务时最 简单的办法就是用一个辅助栈来模拟递归。首先我们把树的头结点放入栈中。在循环中,只要栈不为空,弹出栈的栈顶结点,交换它的左右子树。如果它有左子树, 把它的左子树压入栈中;如果它有右子树,把它的右子树压入栈中。这样在下次循环中就能交换它儿子结点的左右子树了。参考代码如下:
23
24
25
26
27
28
29
30
31
<code>// Mirror a BST (swap the left right child of each node) Iteratively</code>
<code>// Input: pTreeHead: the head of BST</code>
<code>void</code> <code>MirrorIteratively(BSTreeNode *pTreeHead)</code>
<code> </code><code>if</code><code>(!pTreeHead)</code>
<code> </code><code>std::stack<BSTreeNode *> stackTreeNode;</code>
<code> </code><code>stackTreeNode.push(pTreeHead);</code>
<code> </code><code>while</code><code>(stackTreeNode.size())</code>
<code> </code><code>{</code>
<code> </code><code>BSTreeNode *pNode = stackTreeNode.top();</code>
<code> </code><code>stackTreeNode.pop();</code>
<code> </code><code>// swap the right and left child sub-tree</code>
<code> </code><code>BSTreeNode *pTemp = pNode->m_pLeft;</code>
<code> </code><code>pNode->m_pLeft = pNode->m_pRight;</code>
<code> </code><code>pNode->m_pRight = pTemp;</code>
<code> </code><code>// push left child sub-tree into stack if not null</code>
<code> </code><code>if</code><code>(pNode->m_pLeft)</code>
<code> </code><code>stackTreeNode.push(pNode->m_pLeft);</code>
<code> </code><code>// push right child sub-tree into stack if not null</code>
<code> </code><code>if</code><code>(pNode->m_pRight)</code>
<code> </code><code>stackTreeNode.push(pNode->m_pRight);</code>
<code> </code><code>}</code>
本文转自夏雪冬日博客园博客,原文链接:http://www.cnblogs.com/heyonggang/p/3406981.html,如需转载请自行联系原作者