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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T07:49:44+00:00 2026-05-31T07:49:44+00:00

How to find the vertical sum of a binary tree. For example, Consider the

  • 0

How to find the vertical sum of a binary tree.

For example,
Consider the binary tree below,

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

For the above tree, Vertical sum should be calculated as follows,

  • Line 1: 5
  • Line 2: 4
  • Line 3: 2,9,1
  • Line 4: 5
  • Line 5: 1,3,6
  • Line 6: 6
  • Line 7: 3,7,5
  • Line 8: 7
  • Line 9: 5

Output should be:

5,4,12,5,10,6,15,7,5
  • 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-31T07:49:46+00:00Added an answer on May 31, 2026 at 7:49 am

    First you should find the positions, you can do this by counting number of left and rights spend to reach specific node:

                     1     : l = 0, r = 0
                    / \
                   /   \
          l=1,r=0 2     3  : l = 0, r = 1.
                 / \   / \
         ...    4...5 6...7 ....
    

    Simply you can traverse your binary tree and finally calculate LorR = NumberOfLeft - NumberOfRights for each node, then group this numbers (by their LorR value) together and find each groups sum (print them from most positive to most negative value of LorR).

    Update: This doesn’t answers for tree of height more than two, we can fix this problem with little modification in algorithm.

    We can see tree as pyramid, each vertex of pyramid has length 1, after each branch remaining part of branch is equal to what passed in latest move, we show this in picture for tree of height 3:

                      1
                    /  \
                   /    \
                  /      \
                 2        3    upto this we used 1/2 size of pyramid
                / \      / \
               /   \    /   \
               4   5    6    7  upto this we used 1/2 + 1/4 part of pyramid
              / \ / \  / \  / \
             5  9 1  3 6 7 5   5  upto this we used 1/2 + 1/4 + 1/4 part of pyramid
    

    This means in each step we calculate left values by their height (in fact each time multiply of 1/2 will be added to left value, except last time, which is equal to h-1 st value).

    So for this case we have: 1 in root is in group 0, 3 in leaf is in group -1/2 + 1/4 + 1/4 = 0, 6 in leaf is in group 1/2 – 1/4 – 1/4 = 0

    1 in leaf is in -1/2 + 1/4 – 1/4 = -1/2 and so on.

    For preventing from rounding of 1/(2^x) to zero or other problems we can multiply our factors (1/2, 1/4, 1/8,…) to 2h-1. In fact in the first case I wrote we can say factors are multiplied by 22-1.

    Related Pyramid for tree of height 4

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working with images like: https://i.stack.imgur.com/zmQr0.jpg I've managed to find a vertical line between
I have a RichTextBox and I need to find the position of the vertical
HEllo , i can not find any way to get horizontal or/and vertical resolution
I am trying to find a good vertical text scroller. Basically what I want
The CSS2 box model tells us that adjoining vertical margins collapse . I find
How can I find out what percentage of the vertical scrollbar a user has
Can't find anchor property in WPF, was it reconstructed to Horizontal and Vertical Alignments?
On hover of a HyperlinkButton in a vertical StackPanel, controls below the HyperlinkButton inch
I am trying to find the best way to parse a line that looks
Referring to Menu / Scrolling Example I would like to create / find the

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.