Skip to main content

Posts

Showing posts with the label Java

Find children of a given binary tree by target node or by target value

Using Target Node: To print the children of a given binary tree node in Java, you can use a recursive approach. Here's an example code snippet that demonstrates this: class Node { int data; Node left, right; public Node(int item) { data = item; left = right = null; } } class BinaryTree { Node root; public BinaryTree() { root = null; } // Function to print the children of a given node void printChildren(Node node) { if (node == null) { System.out.println("Node is null"); return; } if (node.left != null) System.out.println("Left child: " + node.left.data); else System.out.println("No left child"); if (node.right != null) System.out.println("Right child: " + node.right.data); else System.out.println("No right child"); } public static void mai...