1. /**
    2. * Definition for a binary tree node.
    3. * function TreeNode(val, left, right) {
    4. * this.val = (val===undefined ? 0 : val)
    5. * this.left = (left===undefined ? null : left)
    6. * this.right = (right===undefined ? null : right)
    7. * }
    8. */
    9. /**
    10. * @param {TreeNode} root
    11. * @return {void} Do not return anything, modify root in-place instead.
    12. */
    13. var flatten = function (root) {
    14. const list = []
    15. const helper = (node) => {
    16. if (!node) return
    17. list.push(node)
    18. helper(node.left)
    19. helper(node.right)
    20. }
    21. helper(root)
    22. for(let i = 1; i < list.length; i++) {
    23. const pre = list[i - 1],
    24. cur = list[i]
    25. pre.right = cur
    26. pre.left = null
    27. }
    28. };