📜  完整二叉树镜像中的边缘数

📅  最后修改于: 2021-05-24 21:14:41             🧑  作者: Mango

给定一个完整的深度为H的二叉树。如果从该树的左侧和右侧拍摄了镜像,则:

任务是在拍摄最后一棵树中的两个镜像后找到边缘的数量。

例子:

方法:在每个镜像之后,维护最左边,最右边的节点。每次镜像操作后,边缘数量都会改变。原来,

     $$    No.\hspace{1mm} of\hspace{1mm} nodes = 2^{(H+1)}-1 $$ $$ No.\hspace{1mm} Of\hspace{1mm} edges = 2\times(2^{H}-1}) $$ $$ No.\hspace{1mm} of\hspace{1mm} Left\hspace{1mm} side\hspace{1mm} nodes = H+1 $$ $$ No.\hspace{1mm} of\hspace{1mm} Right\hspace{1mm} side\hspace{1mm} nodes = H+1 $$

右镜像后:

     $$  No.\hspace{1mm} Of\hspace{1mm} edges = (Initial\hspace{1mm}edges\times 2+rightmost \hspace{1mm}nodes) $$

左镜像后:

     $$  No.\hspace{1mm} Of\hspace{1mm} edges = (Initial\hspace{1mm}edges\times 2+leftmost \hspace{1mm}nodes) $$

在完整的修改树中:

     $$  No.\hspace{1mm} Of\hspace{1mm} edges = (Initial\hspace{1mm}edges\times 3+leftmost \hspace{1mm}nodes+rightmost \hspace{1mm}nodes) $$

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
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;
}


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..


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


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


输出:
10