# Calculate a tree

Terence hiu

Ranch Hand

Posts: 36

posted 3 years ago

- 0

Say i have a tree structure created in java

root= +

root left node = +

rood right node = 4

root left of left =2

root left of right =3

(2+3)+4

how would i calculate this tree?

root= +

root left node = +

rood right node = 4

root left of left =2

root left of right =3

(2+3)+4

how would i calculate this tree?

posted 3 years ago

If I were to do it, I would choose to do it recursively. Encountering a number would be the base condition, where the number is simply returned. Encountering an operator will trigger two recursive calls to get the operands which will be needed to calculate the value to be returned.

Henry

- 0

Terence hiu wrote:Say i have a tree structure created in java

root= +

root left node = +

rood right node = 4

root left of left =2

root left of right =3

(2+3)+4

how would i calculate this tree?

If I were to do it, I would choose to do it recursively. Encountering a number would be the base condition, where the number is simply returned. Encountering an operator will trigger two recursive calls to get the operands which will be needed to calculate the value to be returned.

Henry

Terence hiu

Ranch Hand

Posts: 36

posted 3 years ago

Thankyou, problem solved

- 0

Henry Wong wrote:Terence hiu wrote:Say i have a tree structure created in java

root= +

root left node = +

rood right node = 4

root left of left =2

root left of right =3

(2+3)+4

how would i calculate this tree?

If I were to do it, I would choose to do it recursively. Encountering a number would be the base condition, where the number is simply returned. Encountering an operator will trigger two recursive calls to get the operands which will be needed to calculate the value to be returned.

Henry

Thankyou, problem solved

Don't get me started about those stupid light bulbs. |