public class TreePrint2 { public static void main(String[] args) { Node root = new Node(15, new Node(12, new Node(6), new Node(14) ), new Node(40, new Node(25), new Node(80) ) ); //Print the tree. print(root, 0); System.exit(0); } //Print the tree rooted at node. private static void print(Node node, int indent) { if (node != null) { print(node.left, indent + 1); System.out.println(" ".repeat(indent) + node.value); print(node.right, indent + 1); } } } /* Try the other orders: //Swap left and right. print(node.right, indent + 1); System.out.println(" ".repeat(indent) + node.value); print(node.left, indent + 1); //Print the root first, followed by the left and right asubtrees. System.out.println(" ".repeat(indent) + node.value); print(node.left, indent + 1); print(node.right, indent + 1); */ class Node { int value; Node left; Node right; Node(int value, Node left, Node right) { this.value = value; this.left = left; this.right = right; } Node(int value) { this.value = value; //this.left and this.right implicitly initialized to null } };