Search code examples
javatreebinary-treebinary-search-tree

How this code is running without any return value at the end in java? Please Explain?


can you please explain to me this code why we don't have any return type in this and the code is still working fine?

public static void printBetweenK1K2(BinaryTreeNode<Integer> root, int k1, int k2) {
    
    if (root == null) {
        return;
    }

    if (root.data >= k1 && root.data <= k2) {
        System.out.println(root.data);
    }
    
    if (root.data > k1) {
        printBetweenK1K2(root.left, k1, k2);
    }
    
    if (root.data <= k2) {
        printBetweenK1K2(root.right, k1, k2);
    }
    
}   

Solution

  • void methods in java don't need to return a thing. Read this for better understanding.