Trying to print the closest sibling in a BST. How can I improve on this algorithm in terms of space/time efficiency? Please point out any potential bugs too! I'd appreciate that.import java.util.ArrayList;    import java.util.List;    public class ClosestSibling {        public static void printClosestSibling(Node node, List<Node> levelNodes) {            if (node == null) return;            levelNodes.add(node);            while (!levelNodes.isEmpty() || !(levelNodes.size()==0)) {                printSiblings(levelNodes);                levelNodes=replaceWithChildNodes(levelNodes);            }        }        public static List<Node> replaceWithChildNodes(List<Node> levelNodes) {            List<Node> newLevelNodes = new ArrayList<Node>();            for (Node node : levelNodes) {                if (node.left != null) {                    newLevelNodes.add(node.left);                }                if (node.right != null) {                    newLevelNodes.add(node.right);                }            }            return newLevelNodes;        }        public static void printSiblings(List<Node> levelnodes) {            for (int i = 0; i < levelnodes.size() - 1; i++) {                System.out.println(levelnodes.get(i).data + : + levelnodes.get(i + 1).data);            }            System.out.println(levelnodes.get(levelnodes.size()-1).data + : + null);        }        public static void main(String[] args) {            BinarySearchTree bst = new BinarySearchTree();            bst.root = bst.addNode(10, bst.root);            bst.addNode(20, bst.root);            bst.addNode(5, bst.root);            bst.addNode(2, bst.root);            bst.addNode(40, bst.root);            bst.addNode(15, bst.root);            bst.addNode(8, bst.root);            System.out.println();            bst.printTree(bst.root);            System.out.println();            printClosestSibling(bst.root, new ArrayList<Node>());        }    }
Finding Closest Sibling in a BST efficiently
java;interview questions
while (!levelNodes.isEmpty() || !(levelNodes.size()==0)) {This is duplicate code; isEmpty checks for size() == 0.System.out.println(levelnodes.get(levelnodes.size()-1).data + : + null);Here, in printSiblings, you can get IndexOutOfBoundsException if the list is empty.I also don't understand why printClosestSibling takes a List<Node> argument. Either you want to take 1 node to print the closestSibling of, in which case you should remove the argument and accept a single node, or you want to take a list of nodes, in which case I'd recommend overloading:public static void printClosestSibling(Node... nodes){    printClosestSibling(Arrays.asList(nodes));}public static void printClosestSibling(List<Node> nodes){    ...}Keep in mind that you need to also deal with the fact that levelNodes could contain null. You would get NullPointerException right now. Either explain via the documentation, or fail-fast and check the list of nodes for null before running your entire algorithm.