LeetCode: N Ary Tree Preorder Traversal Solution
Implementation
1/**2 * // Definition for a Node.3 * function Node(val, children) {4 * this.val = val;5 * this.children = children;6 * };7 */89/**10 * @param {Node} root11 * @return {number[]}12 */13var preorder = function (root) {14 const res = []1516 const traverse = node => {17 if (!node) return1819 res.push(node.val)20 node.children.forEach(traverse)21 }2223 traverse(root)2425 return res26}
References
Comments
Loading comments...
Tags
leetcode
tree
Apply and earn a $2,500 bonus once you're hired on your first job!
Clients from the Fortune 500 to Silicon Valley startups
Choose your own rate, get paid on time
From hourly, part-time, to full-time positions
Flexible remote working environment
A lot of open JavaScript jobs!!
Fact corner: Referred talent are 5x more likely to pass the Toptal screening process than the average applicant.
Still hesitate? Read HoningJS author's guide on dealing with Toptal interview process.
Next Post
LeetCode: Triangle
Memoized recursion