const tree ={value:1,children:[{value:2,children:[{value:4,children:[]},{value:5,children:[]}]},{value:3,children:[{value:6,children:[]},{value:7,children:[]}]}]};
functiontreeToList(tree){let result =[];functiondfs(node){
result.push(node.value);// Add current node's value to the list
node.children.forEach(child=>dfs(child));// Recursively call dfs on each child}dfs(tree);// Start DFS from the root nodereturn result;}// Example usage:const list =treeToList(tree);
console.log(list);// Output: [1, 2, 4, 5, 3, 6, 7]
functiontreeToListBFS(tree){let result =[];let queue =[tree];// Initialize the queue with the root nodewhile(queue.length >0){let node = queue.shift();// Dequeue the first node
result.push(node.value);// Add the node's value to the result list
queue.push(...node.children);// Enqueue all children of the current node}return result;}// Example usage:const listBFS =treeToListBFS(tree);
console.log(listBFS);// Output: [1, 2, 3, 4, 5, 6, 7]