Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7522555
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T02:33:26+00:00 2026-05-30T02:33:26+00:00

Given a binary tree with n leaves and a set of C colors. Each

  • 0

Given a binary tree with n leaves and a set of C colors. Each leaf node of the tree is given a unique color from the set C. Thus no leaf nodes have the same color. The internal nodes of the tree are uncolored. Every pair of colors in the set C has a cost associated with it. So if a tree edge connects two nodes of colors A and B, the edge cost is the cost of the pair (A, B). Our aim is to give colors to the internal nodes of the tree, minimizing the total edge cost of the tree.

I have working on this problem for hours now, and haven’t really come up with a working solution. Any hints would be appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T02:33:27+00:00Added an answer on May 30, 2026 at 2:33 am

    I am going to solve the problem with pseudocode, because I tried writing explanation and it was completely not understandable even for me. Hopefully the code will do the trick. The complexity of my solution is not very good – memory an druntime in O(C^2 * N).

    I will need couple of arrays I will be using in dynamic approach to your task:
    dp [N][C][C] -> dp[i][j][k] the maximum price you can get from a tree rooted at node i, if you paint it in color j and its parent is colored in color k
    maxPrice[N][C] -> maxPrice[i][j] the maximum price you can get from a tree rooted in node i if its parent is colored in color j
    color[leaf] -> the color of the leaf leaf
    price[C][C] -> price[i][j] the price you get if you have pair of neighbouring nodes with colors i and j
    chosenColor[N][C] -> chosenColor[i][j] the color one should choose for node i to obtain maxPrice[i][j]

    Lets assume the nodes are ordered using topological sorting, i.e we will be processing first leaves. Topological sorting is very easy to do in a tree. Let the sorting have given a list of inner nodes inner_nodes

    for leaf in leaves:
       for i in 0..MAX_C, j in 0..MAX_C
           dp[leaf][i][j] = (i != color[leaf]) ? 0 : price[i][j]
       for i in 0..MAX_C,
           maxPrice[leaf][i] = price[color[leaf]][i]
           chosenColor[leaf][i] = color[leaf]
    for node in inner_nodes
       for i in 0..MAX_C, j in 0..MAX_C
           dp[node][i][j] = (i != root) ? price[i][j] : 0
           for descendant in node.descendants
               dp[node][i][j] += maxPrice[descendant][i]
       for i in 0...MAX_C
           for j in 0...MAX_C
             if maxPrice[node][i] < dp[node][j][i]
                 maxPrice[node][i] = dp[node][j][i]
                 chosenColor[node][i] = j
    
    for node in inner_nodes (reversed)
       color[node] = (node == root) ? chosenColor[node][0] : chosenColor[node][color[parent[node]]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose in a given binary tree if each node contains number of child elements
Given the binary tree shown below, determine the order in which the nodes of
Assuming a complete binary tree, each node can be adressed with the position it
I am implementing a tree which is a Binary Expression Tree. The leaf nodes
For a given binary tree, find the largest subtree which is also binary search
Given a simple binary tree, how can we prove that tree is a binary
1) Given 2 arrays containing elements of a complete Binary tree(level by level), without
I'm trying to construct a binary tree (unbalanced), given its traversals. I'm currently doing
A binary tree T is semi-balanced if for every node m in T: R(m)/2
Consider the following array, which is claimed to have represented a binary tree: [1,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.