将二叉树转换为其镜像树的 C 函数
C Function to Convert a Binary Tree into its Mirror Tree
如何将树转换为其镜像树。例如
1 1
/ \ / \
2 3 to 3 2
/ \
4 4
执行post次序遍历。
void mirror(struct node* node)
{
if (node!=NULL)
{
struct node* temp;
/* do the subtrees */
mirror(node->left);
mirror(node->right);
/* swap the pointers in this node */
temp = node->left;
node->left = node->right;
node->right = temp;
}
}
Solution uses recursion to convert a tree to its mirror tree.
- If root of both trees are null, then they are same. Return true.
- If roots of both the trees are not null, check if the data in the two nodes is same and recursively check if left and right subtrees are identical.
- If the roots of only one of the trees is null, then the trees are not identical, so return false.
如何将树转换为其镜像树。例如
1 1
/ \ / \
2 3 to 3 2
/ \
4 4
执行post次序遍历。
void mirror(struct node* node)
{
if (node!=NULL)
{
struct node* temp;
/* do the subtrees */
mirror(node->left);
mirror(node->right);
/* swap the pointers in this node */
temp = node->left;
node->left = node->right;
node->right = temp;
}
}
Solution uses recursion to convert a tree to its mirror tree.
- If root of both trees are null, then they are same. Return true.
- If roots of both the trees are not null, check if the data in the two nodes is same and recursively check if left and right subtrees are identical.
- If the roots of only one of the trees is null, then the trees are not identical, so return false.