Computing Binary Tree Sums without Globals/Statics

Click For Summary

Discussion Overview

The discussion revolves around developing an algorithm to compute the sum of the keys of nodes in a binary tree without using global or static variables. Participants share their code attempts and seek feedback on correctness.

Discussion Character

  • Technical explanation

Main Points Raised

  • One participant presents an algorithm and asks for validation, noting the recursive structure and the summation of node values.
  • Another participant shares a similar algorithm, claiming it is correct without further elaboration.
  • A question arises regarding the variable 's1' in one of the code snippets, leading to clarification that it was intended to refer to 'm'.
  • One participant confirms the correctness of the algorithm presented by another, expressing appreciation.

Areas of Agreement / Disagreement

There is a general agreement on the correctness of the algorithms presented, though the discussion includes some clarifications and corrections regarding variable naming.

Contextual Notes

Some assumptions about variable scope and naming conventions are present, but they are not fully resolved within the discussion.

evinda
Gold Member
MHB
Messages
3,741
Reaction score
0
Hi! (Wave)

I want to write an algorithm, that counts the sum of the keys of the nodes of a binary tree, without the use of globals and statics.

That's what I have tried:

Code:
S(NODE *P){
 if (P==NULL) return 0;
 int m=P->data+S(P->left);
 int n=m+S(P->right);
 return n;

Could you tell me if it is right? (Thinking)
 
Last edited:
Technology news on Phys.org
evinda said:
Hi! (Wave)

I want to write an algorithm, that counts the sum of the keys of the nodes of a binary tree, without the use of globals and statics.

That's what I have tried:

Code:
S(NODE *P){
 if (P==NULL) return 0;
 int s1=P->data+S(P->left);
 int s2=s1+S(P->right);
 return s2;

Could you tell me if it is right? (Thinking)

It is right. (Smile)
 
evinda said:
Code:
S(NODE *P){
 if (P==NULL) return 0;
 int m=P->data+S(P->left);
 int n=s1+S(P->right);
 return n;
What is [m]s1[/m]?
 
Evgeny.Makarov said:
What is [m]s1[/m]?

With [m] s1 [/m] I meant [m]m[/m], I am sorry... (Smile)
 
I like Serena said:
It is right. (Smile)

Great! Thanks a lot! (Party)
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 3 ·
Replies
3
Views
4K
  • · Replies 3 ·
Replies
3
Views
2K
Replies
1
Views
2K
  • · Replies 10 ·
Replies
10
Views
3K
Replies
14
Views
3K
  • · Replies 7 ·
Replies
7
Views
2K
Replies
2
Views
2K
  • · Replies 11 ·
Replies
11
Views
3K