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

📅  最后修改于: 2021-10-28 01:31:22             🧑  作者: Mango

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

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

还给出了一个大小为N的数组arr和一个数字K 。任务是用颜色 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


Javascript


输出:
4

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程