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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T23:32:24+00:00 2026-05-21T23:32:24+00:00

For a binary tree i want to get the sum of all nodes that

  • 0

For a binary tree i want to get the sum of all nodes that fall in a single verticle line. I want the sum of nodes in each verticle node

             A
           /    \
          B      C
         /  \   /  \
         D   E  F   G
        / \ 
        H  I

IF you look at above tee

line 0   A  E F   so sum  = A+E+F
line -1  B I      so sum = B +I
line 1   C        so sum = C
line 2   G        so sum = G

I implemented following algorithm

Map<Integer,Integere> mp = new HashMap<Integer,Integer>()
calculate(root,0); 

 void calculate(Node node, int pos){
   if(node==null)
        return ;
  if(mp.containsKey(pos) ){
    int val = mp.get(pos) + node.data;
     mp.put(pos,val);
    }
    else{ 
         mp.put(pos,node.data);
    }

    calculate(node.left,pos-1);
    calculate(node.right,pos+1);

}
  1. I think the above algo is fine.Can
    any one confirm?
  2. Also how can i do it without using
    HashMap,arraylist or any such
    collection datatype of java.One
    method is two is 2 arrays one for
    storing negative indexes(mapped to
    positive) and one for positive
    indexs(right side of root).But we
    dont know what the size of array
    will be.
  3. One approach is to use doubly link
    list and add a node on right/left
    movement if necessary. Am not
    getting how can i implement this
    approach? Any other simple/more time
    efficient approach?
  4. Is the complexity of the above code
    i imolmeted is O(n)? (am not good at
    analysing time complexity , so
    asking )
  • 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-21T23:32:25+00:00Added an answer on May 21, 2026 at 11:32 pm

    C++ code

    int vertsum(Node* n, int cur_level, int target_level)
    {
      if (!n)
        return 0;
    
      int sum = 0;
      if (cur_level == target_level)
        sum = n->value;
      return sum + 
             vertsum(n->left, cur_level-1, target_level) + 
             vertsum(n->right, cur_level+1, target_level);
    }
    

    invocation example:

    vertsum(root, 0, 1);
    

    EDIT:

    After clarifying the requirements, here the suggested code. Note that this is C++’ish and not exactly using Java’s or C++’s standard API for lists, but you should get the idea. I assume that addNodeBefore and addNodeAfter initialize node’s data (i.e. ListNode::counter)

    void vertsum(TreeNode* n, int level, ListNode& counter)
    {
      if (!n)
        return;
    
      counter.value += n->value;
      counter.index = level;
    
      if (! counter.prev)
        addNodeBefore(counter);
      vertsum(n->left, level-1, counter.prev);             
    
      if (! counter.next)
        addNodeAfter(counter);
      vertsum(n->right, level+1, counter.next);
    
      return;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a binary tree where each node can have a value. I want
I want to create binary search tree data structure in C/C++ where each node
I have a binary tree, that I am searching: TreeNode<Vessel*>* node = this->tree_->search(PotatoFace); string
I have a perfect binary tree, i.e. each node in the tree is either
I have a binary tree class that is created with a root node and
Say I have a binary tree which contains pointers at each node going to
so i want to make a code, that creates a binary tree, that holds
So i want to make a code, that creates a binary tree, that holds
I want to search for an item in non-binary tree (any node can have
I have a Binary tree for a mathematical expression(infix), i want to convert directly

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.