📌  相关文章
📜  在无限的二叉树中找到给定节点的颜色

📅  最后修改于: 2021-05-04 18:24:26             🧑  作者: Mango

给定一个无限长的二叉树,其模式如下:

1
                    /   \
                  2       3
                /  \     /   \ 
               4    5   6     7
             /  \  / \ / \   / \
            ......................

还给出了大小为N且数字为K的数组arr 。任务是依次用颜色i(1 <= i <= N)为节点arr [i]的所有子树着色。给所有子树着色后,找到节点K的颜色。

注意:-最初,所有节点的颜色均为0。

天真的方法是为每个索引i(1 <= i <= N)更新子树中所有节点的颜色,直到节点的级别小于给定节点K的级别。在N次操作之后,将打印节点K的颜色。

一种有效的方法是使用哈希

  • 首先,将所有具有各自颜色的节点插入地图中(以防万一,如果一个节点有多种颜色,则保留最大颜色数)。
  • 查找给定节点k的所有祖先,并输出具有最大数量的颜色。 (如果当前节点是x ,那么(x-1)/ 2x / 2是其祖先)

下面是上述方法的实现:

CPP
// CPP program to find color of the node
#include 
using namespace std;
  
// Function to find color of the node
int findColor(map mapWithColor, int query)
{
    // Maximum is to store maximum color
    int maximum = 0;
  
    // Loop to check all the parent 
    // values to get maximum color
    while (query >= 1) {
  
        // Find the number into map and get maximum color
        if (mapWithColor.find(query) != mapWithColor.end())
        {
            // Take the maximum color and
            // assign into maximum variable
            maximum = max(maximum, mapWithColor[query]);
        }
  
        // Find parent index
        if (query % 2 == 1)
            query = (query - 1) / 2;
        else
            query = query / 2;
    }
  
    // Return maximum color
    return maximum;
}
  
// Function to build hash map with color
map buildMapWithColor(int arr[], int n)
{
    // To store color of each node
    map mapWithColor;
  
    // For each number add a color number
    for (int i = 0; i < n; i++) {
        // Assigning color
        mapWithColor[arr[i]] = i + 1;
    }
  
    // Return hash map
    return mapWithColor;
}
  
// Driver code
int main()
{
    int arr[] = { 3, 2, 1, 3 };
  
    int n = sizeof(arr) / sizeof(arr[0]);
  
    int k = 7;
  
    // Build mapWithColor
    map mapWithColor = buildMapWithColor(arr, n);
  
    // Print the maximum color
    cout << findColor(mapWithColor, k);
  
    return 0;
}


Java
// JAVA program to find color of the node
import java.util.*;
  
class GFG 
{
  
    // Function to find color of the node
    static int findColor(HashMap mapWithColor, int query)
    {
        // Maximum is to store maximum color
        int maximum = 0;
  
        // Loop to check all the parent
        // values to get maximum color
        while (query >= 1)
        {
  
            // Find the number into map and get maximum color
            if (mapWithColor.containsKey(query)) 
            {
                // Take the maximum color and
                // assign into maximum variable
                maximum = Math.max(maximum, mapWithColor.get(query));
            }
  
            // Find parent index
            if (query % 2 == 1)
                query = (query - 1) / 2;
            else
                query = query / 2;
        }
  
        // Return maximum color
        return maximum;
    }
  
    // Function to build hash map with color
    static HashMap buildMapWithColor(int arr[], int n) 
    {
        // To store color of each node
        HashMap mapWithColor = new HashMap();
  
        // For each number add a color number
        for (int i = 0; i < n; i++)
        {
            // Assigning color
            mapWithColor.put(arr[i], i + 1);
        }
  
        // Return hash map
        return mapWithColor;
    }
  
    // Driver code
    public static void main(String[] args)
    {
        int arr[] = { 3, 2, 1, 3 };
  
        int n = arr.length;
  
        int k = 7;
  
        // Build mapWithColor
        HashMap mapWithColor = buildMapWithColor(arr, n);
  
        // Print the maximum color
        System.out.print(findColor(mapWithColor, k));
    }
}
  
// This code is contributed by PrinciRaj1992


Python3
# Python3 code program to find color of the node
  
# Function to find color of the node
def findColor(mapWithColor, query):
  
    # Maximum is to store maximum color
    maximum = 0
  
    # Loop to check all the parent
    # values to get maximum color
    while (query >= 1):
  
        # Find the number into map and get maximum color
        if (query) in mapWithColor.keys():
            # Take the maximum color and
            # assign into maximum variable
            maximum = max(maximum, mapWithColor[query])
  
        # Find parent index
        if (query % 2 == 1):
            query = (query - 1) // 2
        else:
            query = query // 2
  
  
    # Return maximum color
    return maximum
  
# Function to build hash map with color
def buildMapWithColor(arr, n):
    # To store color of each node
    mapWithColor={}
  
    # For each number add a color number
    for i in range(n):
        # Assigning color
        mapWithColor[arr[i]] = i + 1
  
    # Return hash map
    return mapWithColor
  
  
# Driver code
  
arr = [3, 2, 1, 3]
  
n = len(arr)
  
k = 7
  
# Build mapWithColor
mapWithColor = buildMapWithColor(arr, n)
  
# Print the maximum color
print(findColor(mapWithColor, k))
  
# This code is contributed by mohit kumar 29


C#
// C# program to find color of the node
using System;
using System.Collections.Generic;
  
class GFG 
{
  
    // Function to find color of the node
    static int findColor(Dictionary mapWithColor, int query)
    {
        // Maximum is to store maximum color
        int maximum = 0;
  
        // Loop to check all the parent
        // values to get maximum color
        while (query >= 1)
        {
  
            // Find the number into map and get maximum color
            if (mapWithColor.ContainsKey(query)) 
            {
                // Take the maximum color and
                // assign into maximum variable
                maximum = Math.Max(maximum, mapWithColor[query]);
            }
  
            // Find parent index
            if (query % 2 == 1)
                query = (query - 1) / 2;
            else
                query = query / 2;
        }
  
        // Return maximum color
        return maximum;
    }
  
    // Function to build hash map with color
    static Dictionary buildMapWithColor(int []arr, int n) 
    {
        // To store color of each node
        Dictionary mapWithColor = new Dictionary();
  
        // For each number add a color number
        for (int i = 0; i < n; i++)
        {
            // Assigning color
            if(mapWithColor.ContainsKey(arr[i]))
                mapWithColor[arr[i]] = i + 1;
            else
                mapWithColor.Add(arr[i], i + 1);
        }
  
        // Return hash map
        return mapWithColor;
    }
  
    // Driver code
    public static void Main(String[] args)
    {
        int []arr = { 3, 2, 1, 3 };
  
        int n = arr.Length;
  
        int k = 7;
  
        // Build mapWithColor
        Dictionary mapWithColor = buildMapWithColor(arr, n);
  
        // Print the maximum color
        Console.Write(findColor(mapWithColor, k));
    }
}
  
// This code is contributed by Rajput-Ji


输出:
4