'I wrote a recursive method in java to calculate the height of a tree and my question is how can I get this method to print the height one time

So here is my method:

public int getHeight(IntTreeNode root) {
        if(root == null) {
            return 0;
        }
        int height = 1 + Math.max(getHeight(root.left), getHeight(root.right));
         
        System.out.println(height);
        }
        return height;
    }

I know that since it's a recursive method it will print the height in loops until it reaches the final height, but what I want is for this method to print the final height only. Any ideas on how to make this work anyone? thanks.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source