Given a positive integer **N**, the task is to find the count of edges of a perfect binary tree with **N** levels.

**Examples:**

Input:N = 2Output:2 1 / \ 2 3Input:N = 3Output:6 1 / \ 2 3 / \ / \ 4 5 6 7

**Approach:** It can be observed that for the values of **N = 1, 2, 3, …**, a series will be formed as **0, 2, 6, 14, 30, 62, …** whose **N ^{th}** term is

**2**.

^{N}– 2Below is the implementation of the above approach:

## C++

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to return the count ` `// of edges in an n-level ` `// perfect binary tree ` `int` `cntEdges(` `int` `n) ` `{ ` ` ` `int` `edges = ` `pow` `(2, n) - 2; ` ` ` `return` `edges; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `n = 4; ` ` ` ` ` `cout << cntEdges(n); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the approach ` `class` `GFG ` `{ ` ` ` `// Function to return the count ` `// of edges in an n-level ` `// perfect binary tree ` `static` `int` `cntEdges(` `int` `n) ` `{ ` ` ` `int` `edges = (` `int` `)Math.pow(` `2` `, n) - ` `2` `; ` ` ` `return` `edges; ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `int` `n = ` `4` `; ` ` ` ` ` `System.out.println(cntEdges(n)); ` `} ` `} ` ` ` `// This code is contributed by Code_Mech ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation of the approach ` ` ` `# Function to return the count ` `# of edges in an n-level ` `# perfect binary tree ` `def` `cntEdges(n) : ` ` ` ` ` `edges ` `=` `2` `*` `*` `n ` `-` `2` `; ` ` ` ` ` `return` `edges; ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` ` ` `n ` `=` `4` `; ` ` ` ` ` `print` `(cntEdges(n)); ` ` ` `# This code is contributed by AnkitRai01 ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the approach ` `using` `System; ` `class` `GFG ` `{ ` ` ` `// Function to return the count ` `// of edges in an n-level ` `// perfect binary tree ` `static` `int` `cntEdges(` `int` `n) ` `{ ` ` ` `int` `edges = (` `int` `)Math.Pow(2, n) - 2; ` ` ` `return` `edges; ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main(String[] args) ` `{ ` ` ` `int` `n = 4; ` ` ` ` ` `Console.Write(cntEdges(n)); ` `} ` `} ` ` ` `// This code is contributed by Mohit Kumar ` |

*chevron_right*

*filter_none*

**Output:**

14

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:

- Reverse alternate levels of a perfect binary tree
- Reverse alternate levels of a perfect binary tree using Stack
- Number of edges in mirror image of Complete binary tree
- Print Binary Tree levels in sorted order | Set 3 (Tree given as array)
- Averages of Levels in Binary Tree
- Print all K-sum levels in a Binary Tree
- Print all nodes between two given levels in Binary Tree
- Print all Exponential Levels of a Binary Tree
- Left-Right traversal of all the levels of Binary tree
- Print Levels of all nodes in a Binary Tree
- Print all Palindromic Levels Of a Binary Tree
- Print all Co-Prime Levels of a Binary Tree
- Print all Prime Levels of a Binary Tree
- Print Binary Tree levels in sorted order | Set 2 (Using set)
- Maximum sum of leaf nodes among all levels of the given binary tree
- Print Binary Tree levels in sorted order
- Maximum sum of non-leaf nodes among all levels of the given binary tree
- Construct XOR tree by Given leaf nodes of Perfect Binary Tree
- Print odd positioned nodes of odd levels in level order of the given binary tree
- Print even positioned nodes of even levels in level order of the given binary tree

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.