No. 12 - Mirror of Binary Trees
2012-07-13 19:43
369 查看
No. 12 - Mirror of Binary Trees
Problem: Please implement a function which returns mirror of a binary tree.Binary tree nodes are defined as:
struct BinaryTreeNode
{
int m_nValue;
BinaryTreeNode* m_pLeft;
BinaryTreeNode* m_pRight;
};
Analysis: Mirror of binary trees may be a new concept for many candidates, so they may not find a solution in short time during interviews. In order to get some visual ideas about mirrors, we may draw a binary tree and its mirror
according to our daily experience. For example, the binary tree on the right of Figure 1 is the mirror of the left one.
Let us analyze these two trees in Figure 1 carefully to get the steps of mirrors. Their root nodes are identical, but their left and right children are swapped. Firstly we may swap two nodes under the root of the original binary tree, and it becomes the
second tree in Figure 2.
We can notice that the children nodes of not 10 and 6 keep unchanged. Therefore, we need to continue to swap their children nodes. The third and fourth trees in Figure 2 are the swapped binary trees accordingly. It is noticeable that the fourth tree is the
mirror target.
Now we can summarize the process for mirror: We visit all nodes in a binary tree with pre-order traversal, and swap the children of current visited node if it has. We will get mirror of a binary tree after we visit all of its nodes.
Let us begin to write code after we get clear ideas about the process. The following is the sample code:
void Mirror(BinaryTreeNode *pNode)
{
if((pNode == NULL) || (pNode->m_pLeft == NULL && pNode->m_pRight))
return;
BinaryTreeNode *pTemp = pNode->m_pLeft;
pNode->m_pLeft = pNode->m_pRight;
pNode->m_pRight = pTemp;
if(pNode->m_pLeft)
MirrorRecursively(pNode->m_pLeft);
if(pNode->m_pRight)
MirrorRecursively(pNode->m_pRight);
}
The author Harry He owns all the rights of this post. If you are going to use part of or the whole of this ariticle in your blog or webpages, please add a referenced tohttp://codercareer.blogspot.com/.
If you are going to use it in your books, please contact me (zhedahht@gmail.com) . Thanks.
相关文章推荐
- No. 06 - Post-order Traversal Sequences of Binary Search Trees
- 【Leetcode】Given a binary tree, check whether it is a mirror of itself
- error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'std::string'
- Error 1 error C2679: binary '>>' : no operator found which takes a right-hand operand of type 'std::
- 12.从上往下遍历二元树[LevelOrderOfBinaryTree]
- MirrorOfBinaryTree
- 解决 C++错误 error C2679: binary '=' : no operator defined which takes a right-hand operand of type 'class CString' (or there is no
- 【DataStrcutre】Introduction and description of Binary Trees
- 【DataStrcutre】Introduction and description of Binary Trees
- error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'std::wstring'
- error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'std::wstring'
- Distinct binary strings of length n with no consecutive 1s
- [面试题]Amazon: Given two binary trees,if the first tree is subtree of the second one
- The diameter of a binary tree [No. 40]
- Chapter 12 Binary Search Trees
- error C2679: binary '<<' : no operator defined which takes a right-hand operand of type 'class Stude
- error C2678: binary '>>' : no operator found which takes a left-hand operand of type 'std::
- error C2679: binary '<<' : no operator defined which takes a right-hand operand of type '' (or there is no acceptable conversion
- error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'std::string'
- 【GoLang笔记】A Tour of Go - Exercise: Equivalent Binary Trees