Sotheonlythingthat's goingtochangefromheretothegeneraltreeisthatthesetwostepsareactuallygoingtobemergedinto a singlestep, andthat's goingtoberun a helperfunctionorjustrecursivelyonthechildren, allofthem.
And I thinktheorderdoesmatter, soweprobablywanttodotheleftmostchildfirst, andthenthenextone, andthenthenextone.
There's noneedtoreturnanything, becausewhatwewanttodoisbasicallycollectallthevaluesofeverynode, andthenputthemin a list, andthenreturnthat.
Sofromhere, wecouldreturnanemptylistfromhere, andthendo a bunchofjoiningwiththerecursivecall, but I thinkit's probablyjusteasiertohave a variablelikethisresult, andthenwecanjustreturnthatouthere.
Andthenwithinthefunction, wecanmodifythat.
Wecouldhavemadeit a membervariableifweneededto, but I didn't.
Sohere, remember, there's twosteps.
We'regoingtogothrougheverychild.
Sofor a childinnode.children, thatisthedatatypethat's giventous.
I thinkitshouldbe a listtype, butinPython, it's kindofambiguous.
Withthisproblem, I thinkwecouldhavejustgottenthereturnvaluesfromhere, andthenjoinedtheminto a singlelist, butthatwouldn't havebeenasefficient, anditwouldhavebeenmorecomplicatedtocodethatupaswell.
Sothisis, I think, theworkingsolution.
Let's runit.
Okay, well, I promiseyouitwouldbetheworkingsolutionifwedidn't forgettoactuallycallthehelperfunction.
Soletmedothatontheroot.
Reallysorryaboutthat.
Okay, thereyougo.
Itworks.
Thisistechnicallythemostefficientsolution.
We'rejustgoingtogothrougheverynodeinthetree.
Sointermsofthetimecomplexity, itwouldhavebeenbig O of N.
Intermsofthememorycomplexity, itstillwouldhavebeenbig O of H, where H istheheightofthetree.
For a balancedtree, itshouldbelog N.
Itshouldbelogwherethebaseis, I guess, thebranchingfactor, ifthat's thecorrecttermforthis.