I have written the below code for recursively searching binary tree . Even though my system.out statement is getting executed , the return statement is not returning out of entire recursion and thus this method not returning true.
Can anyone suggest how can I return out of entire recursion.?
public static boolean isElementinTree(int num, BinaryTreeNode root) { if (root != null) { int rootVal = root.getData(); BinaryTreeNode left = root.getLeft(); BinaryTreeNode right = root.getRight(); if (left != null) { isElementinTree(num,left); } if (right != null) { isElementinTree(num,right); } if (num == rootVal) { System.out.println("------ MATCH -----"); return true; } } return false; }