Given a complete binary tree of depth **H**. If the mirror image from the left and the right side of this tree is taken then:

Right Mirrored Image:Rightmost node of the every level is connected to mirrored corresponding node.

Left Mirrored Image:Left most node of the every level is connected to mirrored corresponding node.

The task is to find the number of edges after taking both the mirror images in the final tree.

**Examples:**

Input:H = 1

Output:10

2 edges in the original tree will get mirrored in the mirror images (left and right) i.e. 6 edges in total.

And the edges connecting the mirror images with the original tree as shown in the image above.

Input:H = 2

Output:24

(6 * 3) + 3 + 3 = 24

**Approach:** Maintain the leftmost, rightmost nodes after each mirror image. Number of edges will change after each operation of mirror image. Initially,

**After right mirrored image: **

**After left mirrored image: **

**In complete modified tree: **

Below is the implementation of the above approach:

## C++

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to return the total number ` `// of edges in the modified tree ` `int` `countEdges(` `int` `H) ` `{ ` ` ` ` ` `int` `edges, right, left; ` ` ` `edges = 2 * (` `pow` `(2, H) - 1); ` ` ` `left = right = H + 1; ` ` ` ` ` `// Total edges in the modified tree ` ` ` `int` `cnt = (edges * 3) + left + right; ` ` ` `return` `cnt; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `H = 1; ` ` ` ` ` `cout << countEdges(H); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the approach ` `import` `java.io.*; ` ` ` `class` `GFG { ` ` ` ` ` `// Function to return the total number ` ` ` `// of edges in the modified tree ` ` ` `static` `int` `countEdges(` `int` `H) ` ` ` `{ ` ` ` ` ` `int` `edges, right, left; ` ` ` `edges = ` `2` `* (` `int` `)(Math.pow(` `2` `, H) - ` `1` `); ` ` ` `left = right = H + ` `1` `; ` ` ` ` ` `// Total edges in the modified tree ` ` ` `int` `cnt = (edges * ` `3` `) + left + right; ` ` ` `return` `cnt; ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `main(String[] args) ` ` ` `{ ` ` ` `int` `H = ` `1` `; ` ` ` `System.out.println(countEdges(H)); ` ` ` `} ` `} ` ` ` `// This code has been contributed by anuj_67.. ` |

*chevron_right*

*filter_none*

## Python 3

`# Python 3 implementation of the approach ` ` ` `# Function to return the total number ` `# of edges in the modified tree ` `def` `countEdges( H): ` ` ` ` ` `edges ` `=` `2` `*` `(` `pow` `(` `2` `, H) ` `-` `1` `) ` ` ` `left ` `=` `right ` `=` `H ` `+` `1` ` ` ` ` `# Total edges in the modified tree ` ` ` `cnt ` `=` `(edges ` `*` `3` `) ` `+` `left ` `+` `right ` ` ` `return` `cnt ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` `H ` `=` `1` `; ` ` ` ` ` `print` `(countEdges(H)) ` ` ` `# This code is contributed by ChitraNayal ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the approach ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` ` ` `// Function to return the total number ` ` ` `// of edges in the modified tree ` ` ` `static` `int` `countEdges(` `int` `H) ` ` ` `{ ` ` ` ` ` `int` `edges, right, left; ` ` ` ` ` `edges = 2 * (` `int` `)(Math.Pow(2, H) - 1); ` ` ` `left = right = H + 1; ` ` ` ` ` `// Total edges in the modified tree ` ` ` `int` `cnt = (edges * 3) + left + right; ` ` ` `return` `cnt; ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `Main() ` ` ` `{ ` ` ` `int` `H = 1; ` ` ` `Console.WriteLine(countEdges(H)); ` ` ` `} ` ` ` `} ` ` ` `// This code is contributed by AnkitRai01 ` |

*chevron_right*

*filter_none*

**Output:**

10

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Sum of the mirror image nodes of a complete binary tree in an inorder way
- Convert a Binary Tree into its Mirror Tree
- Construct Full Binary Tree using its Preorder traversal and Preorder traversal of its mirror tree
- Create a mirror tree from the given binary tree
- Symmetric Tree (Mirror Image of itself)
- Number of edges in a perfect binary tree with N levels
- Find mirror of a given node in Binary tree
- Check whether a binary tree is a complete tree or not | Set 2 (Recursive Solution)
- Complexity of different operations in Binary tree, Binary Search Tree and AVL tree
- Find number of edges that can be broken in a tree such that Bitwise OR of resulting two trees are equal
- Mirror of n-ary Tree
- Check mirror in n-ary tree
- Check whether a given Binary Tree is Complete or not | Set 1 (Iterative Solution)
- Construct Complete Binary Tree from its Linked List Representation
- Height of a complete binary tree (or Heap) with N nodes
- Find the largest Complete Subtree in a given Binary Tree
- Iterative Boundary Traversal of Complete Binary tree
- Print path from root to all nodes in a Complete Binary Tree
- Sideways traversal of a Complete Binary Tree
- Generate Complete Binary Tree in such a way that sum of non-leaf nodes is minimum

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.