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 8283055
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T10:35:57+00:00 2026-06-08T10:35:57+00:00

I found an interesting algorithmic problem. We are given a binary tree which has

  • 0

I found an interesting algorithmic problem. We are given a binary tree which has value 0 in every vertex apart from leaves. In leaves we have two options:

  1. value is unknown, but we know it is a natural number >=1

  2. value is known and it is a natural number >=1

The problem is to decide if it is possible to set every unknown value in leaves such that every subtree of a given tree has the same sum of values in vertexes in its left and right subtree.

For example:

tree1:

      0
     / \
    0   ?
   / \
  0   5
 / \
?   ?

The answer is NO – taking into consideration that every question mark has to be a natural number, it is of course not possible

tree2:

        0
     /     \
    0      0
   / \    / \
  0   10 ?   ?
 / \
5   ?

The answer is YES – we set: 5, 10, 10 respectively in every question mark.

So far, I came up only with an obvious algorithm – we create system of linear equations and check if it has a solution in natural numbers. But I think it can be very slow for big trees and it should be a better way to solve it. Can anybody help? I would be very grateful.

  • 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-06-08T10:35:59+00:00Added an answer on June 8, 2026 at 10:35 am

    I think a recursive solution works just fine. At every node get the weight of the left and right children. You have the following cases:

    1. L and R are both known: The node is valid iff L == R
    2. Neither L or R is known: This node is unknown and has multiplicity
      twice that of the largest multiplicity of L and R
    3. Exactly one of L or R is unknown: This node is valid iff the
      weight of the known child is divisible by the multiplicty of the
      unknown child. It’s weight is twice the weight of the known child.

    The idea here is that you need to keep track of how many unknown children are below a certain node, since values can only be integers. Multiplicity always doubles because, at one node, even if its left child has 4 unknowns and its right has 1 unknown, then the 1 unknown will have to be a multiple of 4 and so the multiplicity of this node will need to be 8.

    Note: I’m using the word multiplicity here and it isn’t really quite right, but I couldn’t think of a good word to use.

    Here is code, in Go, that demonstrates this solution on your examples:

    package main
    
    import (
      "fmt"
    )
    
    // Assume that (Left == nil) == (Right == nil)
    type Tree struct {
      Val         int
      Left, Right *Tree
    }
    
    func (t *Tree) GetWeight() (weight int, valid bool) {
      if t.Left == nil {
        return t.Val, true
      }
      l, lv := t.Left.GetWeight()
      r, rv := t.Right.GetWeight()
      if !lv || !rv {
        return 0, false
      }
      if l < 0 && r < 0 {
        if l < r {
          return 2 * l, true
        }
        return 2 * r, true
      }
      if l < 0 {
        return 2 * r, r%(-l) == 0
      }
      if r < 0 {
        return 2 * l, l%(-r) == 0
      }
      return r + l, r == l
    }
    
    func main() {
      t := Tree{0,
        &Tree{0,
          &Tree{0,
            &Tree{Val: 5},
            &Tree{Val: -1},
          },
          &Tree{Val: 10},
        },
        &Tree{0,
          &Tree{Val: -1},
          &Tree{Val: -1},
        },
      }
      w, v := t.GetWeight()
      fmt.Printf("%d, %t\n", w, v)
      t = Tree{0,
        &Tree{0,
          &Tree{0,
            &Tree{Val: -1},
            &Tree{Val: -1},
          },
          &Tree{Val: 5},
        },
        &Tree{Val: -1},
      }
      w, v = t.GetWeight()
      fmt.Printf("%d, %t\n", w, v)
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

found a interesting problem during testing our web application. I have application on localhost
Making a migration from 2.8.1 to 2.9.1 found interesting thing. Tried to write this
I found today interesting piece of code: auto ch = (double(), (float(), int()))[\t\a\r\n\0][abcdefghij]; which
Here is a very interesting java problem I've found: Before book printing was found
I have found interesting Ember.js framework which uses Handlebars.js for view templates. However, my
Some days ago I looked at boost sources and found interesting typedef . There
I found this interesting behavior. Why could this happen I am using the cmdlet
I found an interesting bug in a program that I implemented somewhat lazily, and
I found this interesting site, that helps me understand derivatives, http://www.numberempire.com/derivatives.php , It seems
I found some interesting post about memory usage and CPU usage here on Stack

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.