Tree: Inorder Traversal

  • + 0 comments

    Performs an in-order traversal of a binary tree.

    public static void inOrder(Node root) {
        // Base Case: If the node is null, return (end recursion)
        if (root == null) {
            return;
        }
    
        // Recursive Step: Traverse the left subtree
        inOrder(root.left);
    
        // Process the current node (print its value)
        System.out.print(root.data + " ");
    
        // Recursive Step: Traverse the right subtree
        inOrder(root.right);
    }