-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathD78_Mirror tree.cpp
More file actions
30 lines (24 loc) · 844 Bytes
/
D78_Mirror tree.cpp
File metadata and controls
30 lines (24 loc) · 844 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
// Given a binary tree, convert the binary tree to its Mirror tree.
// Mirror of a Binary Tree T is another Binary Tree M(T) with left and right children of all non-leaf nodes interchanged.
// Examples:
// Input: root[] = [1, 2, 3, N, N, 4]
// Output: [1, 3, 2, N, 4]
// Explanation:
// In the inverted tree, every non-leaf node has its left and right child interchanged.
// Input: root[] = [1, 2, 3, 4, 5]
// Output: [1, 3, 2, N, N, 5, 4]
// Explanation:
// In the inverted tree, every non-leaf node has its left and right child interchanged.
class Solution
{
public:
// Function to convert a binary tree into its mirror tree.
void mirror(Node *node)
{
if (!node)
return;
swap(node->left, node->right);
mirror(node->left);
mirror(node->right);
}
};