Creating a structured category list in F# This takes a list of categories and organizes them into a tree-type structure based on their parentId.  This code works, but is obviously a little over-complicated. How can I optimize it?

type ValidString = | ValidString of string | NotValid

type CategoryStructure = {
    Id: ValidString;
    ParentId: ValidString;
    Name: ValidString;
    Abbreviation: ValidString;
    Description: ValidString;
    SapId: ValidString;
    Section: ValidString;
    SectionPosition: ValidString
}

type DynamicCategories = {
    Category: CategoryStructure;
    SubCategories: seq&lt;DynamicCategories&gt;
}

let rec private structureCategories (fullList: CategoryStructure list) 
    (list: CategoryStructure list)  =

    List.fold (fun acc elem -&gt; 


                    // get all categories and details
                    let categories = fullList
                    let mainAcc =
                        [

                            for row in categories do
                                if row = elem
                                then
                                    let subs =  
                                        List.fold (fun acc' elem' -&gt;

                                                    if row.Id = elem'.ParentStructureId
                                                    then
                                                        let modifiedList = elem' :: List.empty&lt;CategoryStructure&gt;
                                                        let foundSubCategory = 
                                                            {
                                                                Category = elem';
                                                                SubCategories = structureCategories fullList modifiedList |&gt; Seq.ofList
                                                            }
                                                        foundSubCategory :: acc'
                                                    else acc'

                                                    ) List.empty&lt;DynamicCategories&gt; categories
                                        |&gt; Seq.ofList        
                                    yield{
                                        Category = elem;
                                        SubCategories = subs
                                    }


                        ]

                    mainAcc @ acc
                    ) List.empty&lt;DynamicCategories&gt; list

// get the initial parent categories and then call the above code to finish the organizing the list
let getStructuredCategories () =

        let categories = allCategoriesAndDetails () |&gt; List.ofSeq
        [
            for row in categories do
                if row.ParentStructureId = NotValid
                then yield row
        ] |&gt; structureCategories categories |&gt; Seq.ofList
Here is an optimized version that I have discovered:

let getStructuredCategories ()  =

let fullList = allCategoriesAndDetails () 

let parentList () =
    allCategoriesAndDetails ()
    |&gt; Seq.filter (fun p -&gt; p.ParentStructureId = NotValid)

let rec toTree (fullList': seq&lt;CategoryStructure&gt;) (parent: CategoryStructure) =

    fullList'
    |&gt; Seq.filter (fun x -&gt; x.ParentStructureId = parent.Id)
    |&gt; Seq.map (fun x -&gt; 
                    {
                        Category = x;
                        SubCategories =
                            toTree fullList' x
                    })
seq {
    for row in parentList () do
    yield {

        Category = row;
        SubCategories = toTree fullList row    
    }

}


Can someone else do better?