Tree: Preorder Traversal

  • + 9 comments

    I have no idea about Java but it was fairly simple to traverse the tree without any knowledge of the language:

    You always start left, then go right. Recurse deeper and print the value

    void Preorder(Node root) {
        System.out.print(root.data + " ");
    
        if (root.left != null) {
            Preorder(root.left);
        } 
        
        if (root.right != null) {
            Preorder(root.right);
        }
    }