# [Programming Problem] N-ary Tree Level Order Traversal

Given an n-ary tree, return the level order traversal of its nodes’ values.

Pretty straightforward BFS traversal logic. Here are some small gotcha’s:

• Add condition for empty/undefined input.
• When you return the list of lists, the expectation is to return just values and not the Node object. Notice the last line (ret.map(..)) is doing just that.
```/** * // Definition for a Node. * function Node(val,children) { * this.val = val; * this.children = children; * }; */ /** * @param {Node} root * @return {number[][]} */ var levelOrder = function(root) { if ( !root || root.length === 0) return []; let ret = [[root]]; let currLevel = 0; while (true) { let nextLevel = []; ret[currLevel].forEach((item) => item.children.forEach((child) => nextLevel.push(child))); if ( nextLevel.length === 0 ) break; ret.push(nextLevel); currLevel++; } return ret.map((arr) => arr.map((item) => item.val)) };```

### 6,976 thoughts on “[Programming Problem] N-ary Tree Level Order Traversal”

2. I together with my friends have been checking out the nice helpful tips found on your site then quickly got a terrible feeling I never expressed respect to you for those secrets. All of the guys were definitely for that reason excited to read through all of them and have in effect in actuality been using those things. I appreciate you for indeed being well considerate and for considering some very good areas most people are really desperate to understand about. Our sincere apologies for not expressing appreciation to sooner.

3. I precisely wanted to appreciate you once more. I do not know the things that I might have tried without the type of ways shared by you relating to such a area of interest. It seemed to be a real hard difficulty in my circumstances, nevertheless coming across a new well-written form you treated it took me to leap with gladness. I’m just happy for the help and thus have high hopes you are aware of an amazing job you are always providing teaching the rest thru your website. I’m certain you haven’t got to know any of us.

4. I precisely desired to thank you so much once more. I am not sure the things that I could possibly have followed in the absence of the actual thoughts shared by you concerning this question. It has been the distressing situation in my circumstances, nevertheless finding out the skilled manner you resolved it made me to weep with fulfillment. I’m just thankful for this help and as well , hope that you really know what a great job you are always carrying out educating people thru your website. Most probably you’ve never got to know any of us.

5. I must show my thanks to you for rescuing me from this dilemma. Right after checking through the the web and seeing concepts which were not pleasant, I figured my life was well over. Being alive minus the solutions to the problems you’ve resolved through your guideline is a critical case, and those which may have adversely affected my career if I had not discovered your website. Your main capability and kindness in touching all areas was helpful. I don’t know what I would have done if I hadn’t encountered such a point like this. I’m able to at this point relish my future. Thanks a lot very much for this skilled and sensible guide. I won’t think twice to propose the blog to anyone who should have tips about this subject.

6. My spouse and i felt now happy Ervin could finish off his investigations because of the ideas he acquired out of the weblog. It’s not at all simplistic to simply possibly be giving freely ideas the rest might have been trying to sell. We really fully grasp we’ve got you to be grateful to for this. Those illustrations you have made, the straightforward web site navigation, the relationships you make it possible to create – it’s most unbelievable, and it’s aiding our son in addition to the family imagine that this idea is excellent, which is certainly particularly mandatory. Many thanks for the whole lot!

7. I would like to voice my gratitude for your generosity giving support to men and women that must have assistance with your theme. Your personal dedication to getting the message throughout was incredibly useful and have specifically encouraged girls much like me to realize their goals. Your personal warm and friendly guidelines implies a lot a person like me and a whole lot more to my colleagues. Regards; from all of us.

8. Thanks a lot for giving everyone an exceptionally brilliant possiblity to read from here. It really is very great and also stuffed with a good time for me personally and my office friends to visit the blog minimum 3 times in one week to find out the fresh guidance you have got. And definitely, we’re certainly happy with the outstanding things you give. Certain 1 ideas in this article are in fact the simplest we have all had.

9. My husband and i felt really thrilled that Albert could carry out his inquiry using the precious recommendations he grabbed when using the web pages. It’s not at all simplistic to simply continually be offering guidance which usually men and women have been selling. And we recognize we need the writer to be grateful to for this. All the illustrations you’ve made, the straightforward blog menu, the relationships you can help to promote – it’s got many awesome, and it’s really aiding our son and our family feel that this subject is brilliant, which is quite serious. Thanks for everything!

10. My husband and i were really excited when Michael could complete his web research by way of the precious recommendations he was given out of the site. It’s not at all simplistic just to find yourself making a gift of instructions that many others have been trying to sell. And we fully grasp we have you to give thanks to because of that. The main explanations you made, the straightforward blog menu, the relationships you help instill – it’s mostly awesome, and it’s assisting our son and us recognize that that concept is brilliant, which is exceedingly mandatory. Thanks for all the pieces!

11. I actually wanted to write a small comment to say thanks to you for these lovely ideas you are giving out at this site. My long internet research has finally been compensated with sensible concept to exchange with my co-workers. I would believe that many of us visitors actually are rather endowed to live in a useful site with very many outstanding people with very helpful pointers. I feel very grateful to have seen your site and look forward to so many more brilliant times reading here. Thank you once more for all the details.

12. I must show my affection for your generosity for those people who absolutely need assistance with this particular topic. Your personal commitment to passing the message throughout was surprisingly valuable and have really encouraged those like me to achieve their ambitions. The invaluable key points indicates a lot to me and much more to my office colleagues. Thank you; from all of us.

13. I have to get across my love for your kindness for all those that require help with this one concern. Your very own dedication to passing the message throughout turned out to be astonishingly good and has usually allowed girls like me to realize their dreams. Your new useful information denotes a lot to me and far more to my office colleagues. Regards; from everyone of us.

14. I wish to convey my appreciation for your generosity giving support to those individuals that should have guidance on in this theme. Your special dedication to getting the solution up and down has been quite good and have surely allowed regular people much like me to achieve their ambitions. The warm and helpful guideline can mean a great deal a person like me and still more to my mates. Many thanks; from everyone of us.

16. I want to voice my admiration for your kindness giving support to individuals that must have assistance with this one issue. Your personal commitment to passing the message along came to be astonishingly insightful and have surely permitted some individuals like me to get to their objectives. Your personal informative information denotes a whole lot to me and somewhat more to my office colleagues. Thanks a lot; from each one of us.

17. I have to get across my respect for your kindness for men and women that actually need guidance on this particular concept. Your personal commitment to getting the solution all-around had been incredibly beneficial and has in most cases allowed workers much like me to achieve their desired goals. Your new interesting useful information implies a whole lot to me and additionally to my colleagues. With thanks; from everyone of us.

18. I wish to get across my appreciation for your kind-heartedness in support of folks that should have guidance on this one matter. Your very own dedication to getting the message around was remarkably important and have made regular people just like me to attain their objectives. Your personal valuable instruction entails this much a person like me and far more to my office workers. Thanks a ton; from each one of us.

19. I want to point out my admiration for your kind-heartedness for those individuals that should have help with your study. Your special dedication to getting the solution all around became quite useful and have continually allowed others much like me to arrive at their desired goals. Your amazing informative advice entails much to me and much more to my mates. Thank you; from each one of us.

20. I am just commenting to make you be aware of of the wonderful discovery my cousin’s princess found visiting your site. She realized numerous details, not to mention how it is like to have a wonderful teaching heart to get other people without problems know just exactly a number of complex subject matter. You really did more than readers’ desires. I appreciate you for coming up with the warm and friendly, dependable, educational and also easy tips about that topic to Sandra.

21. My spouse and i ended up being quite thrilled that Emmanuel managed to finish off his web research via the ideas he obtained through the web pages. It is now and again perplexing to simply possibly be giving out ideas which usually people today have been trying to sell. We recognize we have you to appreciate because of that. The type of explanations you’ve made, the simple web site navigation, the relationships you aid to instill – it is everything superb, and it’s making our son and the family recognize that the idea is interesting, and that’s seriously serious. Thanks for the whole thing!

22. I actually wanted to post a remark to appreciate you for some of the amazing tips you are placing here. My time consuming internet research has finally been rewarded with beneficial know-how to write about with my friends and family. I ‘d believe that we readers are really lucky to exist in a good place with so many outstanding people with interesting advice. I feel very fortunate to have come across the web page and look forward to many more excellent moments reading here. Thanks a lot once again for all the details.

23. I simply desired to say thanks all over again. I am not sure the things that I could possibly have tried without the creative concepts shown by you over my subject matter. It previously was a very distressing situation for me, nevertheless seeing a new specialized technique you handled the issue made me to leap with happiness. I’m just grateful for this advice and thus sincerely hope you find out what an amazing job you’re undertaking educating most people thru a web site. More than likely you have never met all of us.

24. I would like to convey my gratitude for your kindness giving support to individuals that need guidance on this subject. Your personal dedication to passing the message along became amazingly powerful and have in most cases enabled folks much like me to achieve their pursuits. Your own valuable help can mean a great deal to me and far more to my office workers. Warm regards; from everyone of us.

25. I have to convey my gratitude for your kindness for those people who absolutely need guidance on the subject matter. Your very own dedication to passing the solution throughout appears to be surprisingly good and have in most cases enabled professionals like me to attain their objectives. Your new helpful instruction denotes a lot to me and much more to my fellow workers. Many thanks; from everyone of us.

26. My wife and i got now delighted Edward could conclude his homework by way of the ideas he made from your site. It is now and again perplexing to just possibly be giving out instructions which often people today have been trying to sell. And now we take into account we need the blog owner to thank for this. These explanations you’ve made, the straightforward blog navigation, the friendships you can give support to instill – it’s got most remarkable, and it’s really letting our son and the family recognize that this issue is interesting, which is especially mandatory. Thank you for the whole lot!

27. I must point out my admiration for your kind-heartedness for individuals that really need guidance on this important subject matter. Your real dedication to passing the message all through turned out to be certainly important and has surely enabled ladies much like me to get to their ambitions. Your personal interesting guideline implies a whole lot a person like me and a whole lot more to my peers. Best wishes; from each one of us.