How to Calculate Leaf Node Difference in a Binary Tree Using Recursion?

  • Context: Java 
  • Thread starter Thread starter transgalactic
  • Start date Start date
  • Tags Tags
    Binary Tree
Click For Summary

Discussion Overview

The discussion revolves around calculating the difference between the sums of leaf nodes in the left and right subtrees of a binary tree using recursion. Participants explore various recursive approaches and clarify the requirements for counting leaves without introducing additional objects.

Discussion Character

  • Technical explanation
  • Mathematical reasoning
  • Debate/contested

Main Points Raised

  • One participant describes the structure of a binary tree and the goal of calculating the difference between the sums of leaves in the left and right subtrees.
  • Another participant suggests visiting all nodes in the left subtree and counting leaves when both child nodes are null, then doing the same for the right subtree.
  • A clarification is made regarding the counting of leaves, emphasizing the need to count visited leaves rather than increasing an object.
  • A recursive algorithm is proposed, with a base case for leaf nodes returning a height difference of zero, and a method to calculate the height difference recursively.
  • Further clarification is provided on the meaning of counting leaves visited, indicating that reaching a leaf means it should be counted.
  • Another participant argues that a recursive method is preferable and provides a general structure for a method to calculate the difference in the number of leaves between the left and right subtrees.

Areas of Agreement / Disagreement

Participants express differing views on the best approach to count leaves, with some favoring a straightforward counting method and others advocating for a recursive solution. No consensus is reached on a single method or implementation.

Contextual Notes

Participants discuss various implementations and clarify the requirements for counting leaves, but there are unresolved details regarding the exact implementation of the recursive methods and the handling of edge cases.

transgalactic
Messages
1,386
Reaction score
0
there is a given binary tree
each of member are of a Node type
Code:
class Node {
int info;
Node left;
Node right;
}

in the info part of each node we have a number.
in each one of his leaves we have some number
and in each one of its roots (crossroads) we have zeros.

my gole is to build a method which calculates the difference between the
sums of the leaves in the right subtree of T
and the sum of the leaves in the left subtree of T
 
Technology news on Phys.org
just visit all nodes in left sub-tree, if both Node.left && Node.right == null, increase the number of leaves.
Then visit right-subtree and do the same.
 
what do you meen increase the number of leaves
i need to count them i can't add another object
 
Hi Ho!

A recursive algorithm will nicely solve this.
The idea is that we know that the base case will be when a node is a leaf. In this case the height difference will be just 0.
If it is not a base case, simply solve the problem recursively by traversing the left child and the right child.

Here is the code if you do not want to think about how to implement such a solution.

Code:
int calculate_height_difference (Node node)
{
    if (node.left == null && node.right == null)
    {
        return 0;
    }

    return calculate_height_difference (node.right) - calculate_height_difference (node.left);
}


Eus
 
Last edited:
I meant increase the number of leaves VISTIED.
 
comment

>> what do you meen increase the number of leaves

It means you have reached a leave, so count it by increasing the leaf_count
 
yes but a recrusive method is better.

If your classes are:

class TreeNode{

Object data;
TreeNode left;
TreeNode right;

constructor...

}


class Tree{

TreeNode head;

.. other methods..

----------------

So you first have a general method like:

diffLeftRight(){

return Math.abs(numberOfLeaves(head.left) - numberOfLeaves(head.right);

}


numberOfLeaves(TreeNode tr){

if(tr == null)
return 0;
else if(tr.left == null && tr.right == null)
return 1;
else if(tr.left == null && tr.right != null)
return numberOfLeaves(tr.left);
else if(tr.left != null && tr.right == null)
return numberOfLeaves(tr.right);
else
return numberOfLeaves(tr.left) + numberOfLeaves(tr.right);

}

Maybe you need to modify it a bit, but this is the general idea of recursion
}
 

Similar threads

  • · Replies 3 ·
Replies
3
Views
4K
  • · Replies 20 ·
Replies
20
Views
4K
Replies
14
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K