我一直在考虑这个问题,并且我还没有找到一个好的,有效的解决方案。如何在二叉树中找到给定节点(或项目)的镜像节点高效地
如何查找二叉树中给定节点(或项目)的镜像节点?
// Node definition
struct _Node {
char data;
struct _Node* left;
struct _Node* right;
} Node;
// Assumption:
// "given" is guaranteed in the binary tree ("root" which is not NULL)
Node* FindMirrorNode(Node* root, Node* given)
{
// Implementation here
}
// OR:
// Assumption:
// in the binary tree ("root"), there is no repeated items, which mean in each node the char data is unique;
// The char "given" is guaranteed in the binary tree.
char FindMirrorNodeData(Node* root, char given)
{
// Implementation here
}
注意:我不要求如何找到一个给定的树的镜像树:-)
For example, considering the tree below
A
/ \
B C
/ / \
D E F
\ /\
G H I
The mirror node of 'D' is node 'F'; while the mirror node of 'G' is NULL.
感谢。
这可能是一个愚蠢的问题,但你能解释一个镜像节点是什么或至少链接到它的定义? – 2010-07-04 19:23:58
嗨马克,是的,对,它可能是一个愚蠢的问题,但它也可能是更好地理解二叉树的好习惯:-)谢谢。我添加了镜像节点定义的示例。 – 2010-07-04 19:44:55