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

  • Home
  • SEARCH
  • 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 4611156
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T01:13:48+00:00 2026-05-22T01:13:48+00:00

private void sumNode(TNode node) { int sum = 0; if (node == null) return;

  • 0
private void sumNode(TNode node) {
    int sum = 0;
    if (node == null)
        return;

    sumNode(node.getLeft());
    sumNode(node.getRight());
    if (node.getLeft() != null && node.getRight() != null) {
        sum = (node.getData() + node.getLeft().getData() + node.getRight()
                .getData());
    } else if (node.getLeft() != null) {
        sum = (node.getData() + (Integer) node.getLeft().getData());
    } else if (node.getRight() != null) {
        sum = (node.getData() + node.getRight().getData());
    } else {
        sum = 0;
    }
    node.setData(sum);
}

I know my method is totally wrong – I have no idea how to do this.

I want to replace each node value to be the summation of all its descendants, can anyone guide me how to do?

I have ran out ideas for how to solve this problem. Even pseudocode will be appreciated.

The problem is:

  • My tree has: 5 2 1 3 6 8
  • The outcome is: 0 0 2 0 6 13,
  • The expected outcome is: 0 0 4 0 8 20
  • 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-22T01:13:49+00:00Added an answer on May 22, 2026 at 1:13 am

    If you want to include the original value of the node in the sum, then this is very easy with recursion:

    public void sumNode(TNode<E> root) {
        // For empty trees, do nothing.
        if (root == null)
            return;
    
        // Update the left subtree recursively.
        sumNode(root.left);
    
        // Update the right subtree recursively.
        sumNode(root.right);
    
        // At this point, all the elements in the left and right
        // subtrees are already summed up. Now we update the
        // sum in the root element itself.
        if (root.left != null)
            root.item += root.left.item;
        if (root.right != null)
            root.item += root.right.item;
    }
    

    If you do not want to include the original value, then a single recursive pass is not enough because by the time you calculate the value of a non-leaf node N with two leaves L1 and L2, the values in L1 and L2 have already been updated to zeros, so you cannot use the original values of L1 and L2 to store N. If you are allowed to add a new originalItem entry in the node, you can store the original value there, use my solution above and then run a final pass which subtracts the value of originalItem from item for every node in the tree:

    private void preprocessTree(TNode<E> root) {
        if (root == null)
            return;
    
        preprocessTree(root.left);
        preprocessTree(root.right);
        root.originalItem = root.item;
    }
    
    private void processTree(TNode<E> root) {
        if (root == null)
            return;
    
        processTree(root.left);
        processTree(root.right);
        if (root.left != null)
            root.item += root.left.item;
        if (root.right != null)
            root.item += root.right.item;
    }
    
    private void postprocessTree(TNode<E> root) {
        if (root == null)
            return;
    
        postprocessTree(root.left);
        postprocessTree(root.right);
        root.item -= root.originalItem;
    }
    
    public void sumTree(TNode<E> root) {
        preprocessTree(root);
        processTree(root);
        postprocessTree(root);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

private void activateRecords(long[] stuff) { ... api.activateRecords(Arrays.asList(specIdsToActivate)); } Shouldn't this call to Arrays.asList return
private void StringAction(string aString) // method to be called { return; } private void
private void EnsureCurrentlyValid() { //I'm valid if IDataErrorInfo.this[] returns null for every property var
private void save(Rectangle src, Rectangle dest, string fName, int width, int height, Bitmap file)
private void BindCountry() { XmlDocument doc = new XmlDocument(); doc.Load(Server.MapPath(countries.xml)); foreach (XmlNode node in
private void button1_Click(object sender, RoutedEventArgs e) { int i = 0; while (i <
private void button1_Click(object sender, EventArgs e) { int[] ml = new int[10] ( 1,
private void BindDataList() { int userId = Convert.ToInt32(ProfileInfo.GetUserID()); DataList1.DataSource = CatalogAccess.GetAddressByUserID(userId); DataList1.DataBind(); foreach (DataListItem
private void LoadUsersToComboBox() { comboBox1.DataSource = null; comboBox1.DataSource = peopleRepo.FindAllPeople(); /*Returns IQueryable<People>*/ comboBox1.DisplayMember =
private void equal_AxB() { int x = matrix_A.length; int y = matrix_B[0].length; matrix_C =

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.