Home > Enterprise >  Java : binary tree preorder traversal recursion return a result list
Java : binary tree preorder traversal recursion return a result list

Time:08-24

Small Java question on a preorder traversal of a binary tree, using recursion, with a result list of all the elements returned please.

Looking at the web we can see many result on the use of recursion to traverse a tree. However, they all "just print" the nodes, returning nothing:

https://makeinjava.com/recursive-binary-tree-traversal-algorithm-java-preorder-postorderinorder/

public static void preOrderRecursive(Node root) {
    if (null == root) {
        return;
    }
    System.out.printf("%d ", root.data);
    preOrderRecursive(root.left);
    preOrderRecursive(root.right);
}

This is a recursive function

  • Related