1. var connect = function(root) {
    2. if(!root) return root
    3. const list = [[root]],
    4. resList = []
    5. const dfs = (root, list, count = 1) => {
    6. if(!root || (!root.left && !root.right)) return
    7. const current = list[count] = list[count] ? list[count] : []
    8. if(root.left) {
    9. current.push(root.left)
    10. }
    11. if(root.right) {
    12. current.push(root.right)
    13. }
    14. dfs(root.left, list, count + 1)
    15. dfs(root.right, list, count + 1)
    16. }
    17. dfs(root, list)
    18. for(let i = 0; i < list.length; i++) {
    19. for(let j = 0; j < list[i].length; j++) {
    20. list[i][j].next = list[i][j + 1] ? list[i][j + 1] : null
    21. }
    22. }
    23. return root
    24. };